﻿package com.athena.process
{
	import com.cubeia.firebase.io.protocol.CreateTableRequestPacket;
	import com.cubeia.firebase.io.protocol.CreateTableResponsePacket;
	import com.cubeia.firebase.io.protocol.LeaveRequestPacket;
	import com.cubeia.firebase.io.protocol.KickPlayerPacket;
	import com.cubeia.firebase.io.protocol.ServiceTransportPacket;
	import com.cubeia.firebase.util.ParameterUtil;
    import com.cubeia.firebase.io.protocol.Param;
	import flash.events.*;
	import flash.display.*;
	import com.athena.vo.*;
	import com.athena.controller.*;
	import flash.text.TextFormat;
	import flash.net.URLRequest ;
	import flash.net.URLLoader;
	import flash.net.URLVariables ;
	import flash.net.URLRequestMethod ;
	import flash.net.URLRequestHeader ;
	import com.adobe.serialization.json.JSON;
	import flash.text.TextFieldAutoSize;
	import gs.*;
	import gs.easing.*;
	import flash.utils.*;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import com.athena.process.BasicFunction;
	import com.athena.process.ProcessOutTable_Frame2;
	import com.junkbyte.console.Cc;
	import mx.core.FlexMovieClip;
	
	/**
	 * ...
	 * @author ...
	 */
	public class ProcessInTable 
	{
		public static var positionCards:Array = new Array();
		public static var objPosition:Object = new Object();
		public static var cardX : Array = [450, 705, 780, 700, 515, 240, 155, 220] ;
		public static var cardY : Array = [325, 315, 185, 40, 5, 40, 185, 315] ;
		public static var arrMoviePrivate : Array ;
		public static var ArrExitTable : Array ;
		public static var ArrUpdateMoney : Array ;
		
		public static var currTable:ObjectTable = new ObjectTable();
		public static var currPosition : int ; //Vi tri trong van bai		
		public static var AllowExit : Boolean = true ;
		public static var currentAction : String = ""; //Hanh dong hien tai - Boc || Danh		
		public static var clickMark : Number = 500000 ;
		public static var strXacnhan_BG : String = "" ;
		public static var AName : String = "";
		public static var isPopupResult : Boolean = false ;
		/*********************** Sound *******************************/

		public static var startSound : Sound ;
		public static var winSound : Sound ;
		public static var loseSound : Sound ;
		public static var refreshSound : Sound ;
		public static var preURLSound : String = "./flash/Sound/" ;
		
		public static var chessSelected : Chess = null;		
		public static var chessMove : MovieClip ;
		public static var currentTimerRun : int ; //Vitri dieu khien
		public static const xStart : int = 56 ;
		public static const yStart : int = 35 ;
		public static const xRange : int = 60 ;
		public static const yRange : int = 59 ;
		public static const maxCH : int = 3 ;
		public static const maxDL : int = 3 ;
		public static const minVisibleDL : int = 10 ;
		public static var MyWin : int = 0 ;
		public static var OtherWin : int = 0 ;
		public static var demCH : int = 0 ;
		public static var demDL : int = 0 ;
		public static var MyColor : int = 1 ;
		public static var csOther : int = 0 ;
		public static var OtherColor : int  = 2 ;
		public static var demClick : int = 0 ;
		public static var demPlay : int = 0 ;
		public static var ArrCheck : Array = new Array() ;
		
		public static var feeDiscount : Number = 95 ; //Phi trong ban choi ==> %
		public static var objAddMark : Object = new Object() ; //Luu diem de dat cuoc, danh bien gioi,
		
		//Timer
		public static var tChiaBai:Timer = new Timer(1000); //Dieu khien chia bai
		public static var finishTimer:Timer = new Timer(1000) ; //Dieu khien chat
		public static var chieutuongTimer:Timer = new Timer(1000) ; //Dieu khien chat
		public static var mainTimer:Timer = new Timer(1000);//Dieu khien dong ho chay
		public static var TimerInTable:Timer = new Timer(1000);//Dieu khien thoi gian he thong ==> Sau 2 giay hien thi nut thoat ban
		public static var TimerAutoExit:Timer = new Timer(1000); //Dieu khien thoi gian tu dong click sau khi van choi ket thuc
		
		public static var ArrOldPlay : Array = new Array() ;
		private static var arrayTiles : Array;
		private static var marginL : Number = 4, marginT : Number = 17, numTileX : int = 17, numTileY : int = 17;
		private static var typeTile: Boolean = true;
		private static var turn : Boolean;
		private static var clientEnable : Boolean = true;
		private static var arrayTileWin: Array;
		
		public static function StartTimerInTable(evt:TimerEvent) : void { //sau 2s hien thi nut thoat ban
			if (TimerInTable.currentCount == TimerInTable.repeatCount) {
				if (TimerInTable.running)
					TimerInTable.stop() ;
				if (Cocaro.mainStage_mc.currentFrame == 5) {
					if ((currTable.Status != 1) || (Cocaro.isView == true))
						Cocaro.mainStage_mc.btnBackTable.visible = true ;
				}
			}
		}
		public static function GetColor(cs : int) : String { //Lay mau cua cac IP trong ban choi
			for (var i:int = 0; i < currTable.ArrPlayers.length; i++) 
			{
				if (cs == i) continue ;
				//if (currTable.ArrPlayers[cs].PlayerIP == currTable.ArrPlayers[i].PlayerIP) return "Do" ;
			}
			return "Trang" ;
		}
		
		public static function DisplayColorAgain() {
			for (var i:int = 0; i < currTable.ArrPlayers.length; i++) 
			{
				var positionKhung : int = 1 ;
				if (currTable.ArrPlayers[i].Mark > Cocaro.vip) {
					positionKhung = 3 ;
					Cocaro.formatAvatar_Name.color = 0xFFFFFF ;
				} else {
					if (currTable.ArrPlayers[i].Mark > Cocaro.vipSilver)
						positionKhung = 2 ;
					Cocaro.formatAvatar_Name.color = 0xE5DFE9 ;
				}
				Cocaro.formatAvatar_Name.size = 13 ;
				Cocaro.formatAvatar_Mark.size = 12 ;					
				if ((currPosition - i == -1) || (currPosition - i == 7)) {
					Cocaro.mainStage_mc.Player2_mc.Khung_mc.gotoAndStop(positionKhung) ;
					Cocaro.mainStage_mc.Player2_mc.Mark_txt.setTextFormat(Cocaro.formatAvatar_Mark) ;
					Cocaro.mainStage_mc.Player2_mc.Name_txt.setTextFormat(Cocaro.formatAvatar_Name) ;
				} else if ((currPosition - i == -2) || (currPosition - i == 6)) {
					Cocaro.mainStage_mc.Player3_mc.Khung_mc.gotoAndStop(positionKhung) ;
					Cocaro.mainStage_mc.Player3_mc.Mark_txt.setTextFormat(Cocaro.formatAvatar_Mark) ;
					Cocaro.mainStage_mc.Player3_mc.Name_txt.setTextFormat(Cocaro.formatAvatar_Name) ;
				} else if ((currPosition - i == -3) || (currPosition - i == 5)) {
					Cocaro.mainStage_mc.Player4_mc.Khung_mc.gotoAndStop(positionKhung) ;
					Cocaro.mainStage_mc.Player4_mc.Mark_txt.setTextFormat(Cocaro.formatAvatar_Mark) ;
					Cocaro.mainStage_mc.Player4_mc.Name_txt.setTextFormat(Cocaro.formatAvatar_Name) ;
				} else if ((currPosition - i == -4) || (currPosition - i == 4)) {
					Cocaro.mainStage_mc.Player5_mc.Khung_mc.gotoAndStop(positionKhung) ;
					Cocaro.mainStage_mc.Player5_mc.Mark_txt.setTextFormat(Cocaro.formatAvatar_Mark) ;
					Cocaro.mainStage_mc.Player5_mc.Name_txt.setTextFormat(Cocaro.formatAvatar_Name) ;
				} else if ((currPosition - i == -5) || (currPosition - i == 3)) {
					Cocaro.mainStage_mc.Player6_mc.Khung_mc.gotoAndStop(positionKhung) ;
					Cocaro.mainStage_mc.Player6_mc.Mark_txt.setTextFormat(Cocaro.formatAvatar_Mark) ;
					Cocaro.mainStage_mc.Player6_mc.Name_txt.setTextFormat(Cocaro.formatAvatar_Name) ;
				} else if ((currPosition - i == -6) || (currPosition - i == 2)) {
					Cocaro.mainStage_mc.Player7_mc.Khung_mc.gotoAndStop(positionKhung) ;
					Cocaro.mainStage_mc.Player7_mc.Mark_txt.setTextFormat(Cocaro.formatAvatar_Mark) ;
					Cocaro.mainStage_mc.Player7_mc.Name_txt.setTextFormat(Cocaro.formatAvatar_Name) ;
				} else if ((currPosition - i == -7) || (currPosition - i == 1)) {
					Cocaro.mainStage_mc.Player8_mc.Khung_mc.gotoAndStop(positionKhung) ;
					Cocaro.mainStage_mc.Player8_mc.Mark_txt.setTextFormat(Cocaro.formatAvatar_Mark) ;
					Cocaro.mainStage_mc.Player8_mc.Name_txt.setTextFormat(Cocaro.formatAvatar_Name) ;
				} else {
					Cocaro.formatAvatar_Name.size = 14 ;
					Cocaro.formatAvatar_Mark.size = 14 ;
					Cocaro.mainStage_mc.Player1_mc.Khung_mc.gotoAndStop(positionKhung) ;
					Cocaro.mainStage_mc.Player1_mc.Mark_txt.setTextFormat(Cocaro.formatAvatar_Mark) ;
					Cocaro.mainStage_mc.Player1_mc.Name_txt.setTextFormat(Cocaro.formatAvatar_Name) ;					
				}
			}
		}
		/********************************** Load Interface Player ******************************************************/
		public static function LoadData_Player(name : String, mark : Number, url : String, isReady : Boolean, totalGame : int, winGame : int, stt : int, vip : int) : void {
			Cocaro.mainStage_mc["Player" + stt.toString() + "_mc"].visible = true ;
			Cocaro.mainStage_mc["Player" + stt.toString() + "_mc"].Name_txt.text = name;
			Cocaro.mainStage_mc["Player" + stt.toString() + "_mc"].Mark_txt.text = BasicFunction.numberFormat(mark, 0, false, true) + " AG";
			Cocaro.mainStage_mc["Player" + stt.toString() + "_mc"].iconReady_mc.visible = isReady ;
			Cocaro.mainStage_mc["Player" + stt.toString() + "_mc"].Vip_mc.visible = true ;
			Cocaro.mainStage_mc["Player" + stt.toString() + "_mc"].Vip_mc.gotoAndStop(vip + 1) ;
			if (totalGame != 0)
				Cocaro.mainStage_mc["Player" + stt.toString() + "_mc"].Win_txt.text = (int)(winGame * 100 / totalGame) + "%" ;
			else 
				Cocaro.mainStage_mc["Player" + stt.toString() + "_mc"].Win_txt.text = "0%" ;
			Cocaro.mainStage_mc["Player" + stt.toString() + "_mc"].txtTime_mc.text = "" ;
			Cocaro.mainStage_mc["Player" + stt.toString() + "_mc"].btnKick_mc.visible = false ;
			Cocaro.mainStage_mc["Player" + stt.toString() + "_mc"].Chat_mc.visible = false ;
			Cocaro.mainStage_mc["Player" + stt.toString() + "_mc"].Win_txt.visible = false ;
			Cocaro.mainStage_mc["Player" + stt.toString() + "_mc"].Minute_txt.text = "00" ;
			Cocaro.mainStage_mc["Player" + stt.toString() + "_mc"].Second_txt.text = "00" ;
			Cocaro.mainStage_mc["Player" + stt.toString() + "_mc"].Name_txt.setTextFormat(Cocaro.formatAvatar_Name) ;
			Cocaro.mainStage_mc["Player" + stt.toString() + "_mc"].Mark_txt.setTextFormat(Cocaro.formatAvatar_Mark) ;
			if (stt.toString() == "1") {
				Cocaro.mainStage_mc["Player" + stt.toString() + "_mc"].Avatar_mc.addEventListener(MouseEvent.CLICK, EmotionFunction.Avatar1_Kick) ;
			} else if (stt.toString() == "2") {
				Cocaro.mainStage_mc["Player" + stt.toString() + "_mc"].Avatar_mc.addEventListener(MouseEvent.CLICK, EmotionFunction.Avatar2_Kick) ;
			}
			//if (mark > vipMark)
			//	mainStage_mc["Player" + stt.toString() + "_mc"].Khung_mc.gotoAndStop(2) ;
			if (url != null) 
				if (url != "") {
					if (Cocaro.mainStage_mc["Player" + stt.toString() + "_mc"].Avatar_mc.numChildren > 1) {
						Cocaro.mainStage_mc["Player" + stt.toString() + "_mc"].Avatar_mc.removeChildAt(1) ;
					}
					var imageLoader : Loader = new Loader() ;
					var image : URLRequest = new URLRequest(url) ;
					imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoader);				
					imageLoader.name = "TinyAW" ;
					imageLoader.load(image);					
					function onLoader(evt:Event)
					{
						imageLoader.width = 46 ;
						imageLoader.height = 46 ;						
						Cocaro.mainStage_mc["Player" + stt.toString() + "_mc"].Avatar_mc.addChild(imageLoader);
					}					
				}
		}
		/***************************** Sound Kick ***************************************************/
		public static function Sound_Click(myEvent:MouseEvent) { //Kick Tat va bat tieng
			Cocaro.SoundKick() ;
			if (Cocaro.isSound) {
				Cocaro.isSound = false ;
				Cocaro.mainStage_mc.Sound_btn.gotoAndStop("inactive") ;				
			} else {
				Cocaro.isSound = true ;
				Cocaro.mainStage_mc.Sound_btn.gotoAndStop("active") ;				
			}
		}
		/************************************************* Kich player ***************************************************************/
		public static function Check_KickPlayer(name : String) : int { 
			//Check xem co quyen kich nguoi choi khong 0 - Khong co gi, 1 - Hien thi thong bao khong duoc click vi chua du AG, 2- Duoc kick
			//						2 - Hien thi thong bao khong duoc click vi chua du VIP, 3- Nick moi ko duoc kick 4- Duoc kick
			if (Cocaro.mainStage_mc.currentFrame != 5) return 0 ;
			var markMainTable : int = currTable.ArrPlayers[0].Mark ;
			var vipMainTable : int = currTable.ArrPlayers[0].VIP ;
			var markPlayer : int = 0 ;
			var vip : int = 0 ;
			var ik : int = 1 ;
			for (var i:int = 0; i < currTable.ArrPlayers.length; i++) 
			{
				if (currTable.ArrPlayers[i].PlayerName == name) {
					markPlayer = currTable.ArrPlayers[i].Mark  ;
					vip = currTable.ArrPlayers[i].VIP  ;
					ik = currTable.ArrPlayers[i].IK  ;
					break ;					
				}
			}
			//tf.appendText("IK:" + ik) ;
			if ((ik == 1) && (currTable.MarkUnit < 10000)) return 3
			if (vipMainTable > vip) return 4 ; //Vip lon hon duoc kick luon
			if (vip > vipMainTable) return 2 ;
			if (markMainTable < markPlayer) return 1 ;
			if (markPlayer < clickMark) {
				if ((markMainTable - markPlayer) < markPlayer / 10) return 1 ;				
			} else {
				if (markMainTable < markPlayer * 5) return 1 ;
			}
			return 4 ;
		}
		
		public static function KickPlayer(name : String) : void { //Kick Player
			Cocaro.SoundKick() ;
			if (Cocaro.isView) return ;
			var checkKick = Check_KickPlayer(name) ;
			if (checkKick == 1) {
				BasicFunction.ShowAlert("AG của bạn chưa đủ nhiều để kích người này!") ;
			} else if (checkKick == 2) {
				BasicFunction.ShowAlert("Bạn chưa đủ đẳng cấp để kích người này!") ;
			} else if (checkKick == 3) {
				BasicFunction.ShowAlert("Bạn không được phép kích người này!") ;
			}  else if (checkKick == 4) {
				var object : Object = new Object() ;
				object.Name = name ;
				object.evt = "ktable";
				Cocaro.mainSocket.sendData(JSON.encode(object));
				/*object.ID = currTable.TableID ;
				object.StartID = Cocaro.currPlayer.PlayerID ;
				object.Name = name ;
				Cocaro.mainSocket.sendData("KP:" + JSON.encode(object)) ;*/
			}
		}
		
		public static function KickPlayer2(evt : MouseEvent) :void { //Kick Player 2
			KickPlayer(Cocaro.mainStage_mc.Player2_mc.Name_txt.text) ;			
		}
		
		public static function AddBtnReady() : void {
			Cc.log(Cocaro.mainStage_mc.currentFrame);
			if (Cocaro.mainStage_mc.currentFrame != 5) return ;
			var mcCheck : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("btnReady")) ;
			if (mcCheck != null) return ;
			var mcReady : MovieClip ;
			mcReady = MovieClip(new Sansang()) ;
			mcReady.addEventListener(MouseEvent.CLICK, clickReadyTable) ;
			mcReady.name = "btnReady" ;
			mcReady.x = 240 ;
			mcReady.y = 255 ;	
			Cocaro.mainStage_mc.addChild(mcReady) ;
		}
		public static function AddBtnInitGame() : void {
			if (Cocaro.mainStage_mc.currentFrame != 5) return ;
			var mcCheck : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("btnInitGame")) ;
			if (mcCheck != null) return ;
			var mcInitGame : MovieClip ;
			mcInitGame = MovieClip(new Batdau()) ;
			mcInitGame.addEventListener(MouseEvent.CLICK, clickStartTable) ;
			mcInitGame.name = "btnInitGame" ;
			mcInitGame.x = 240 ;
			mcInitGame.y = 255 ;
			Cocaro.mainStage_mc.addChild(mcInitGame) ;
		}
		public static function RemoveBtnRead() : void {
			var tmp : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("btnReady")) ;
			while (tmp != null) {
				Cocaro.mainStage_mc.removeChild(tmp) ;
				tmp = MovieClip(Cocaro.mainStage_mc.getChildByName("btnReady")) ;
			}
		}
		public static function RemoveBtnInitGame() : void {
			var tmp : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("btnInitGame")) ;
			while (tmp != null) {
				Cocaro.mainStage_mc.removeChild(tmp) ;
				tmp = MovieClip(Cocaro.mainStage_mc.getChildByName("btnInitGame")) ;
			}
		}
		/*
		 * Khoi tao thong tin ban
		 * ==> Add thong tin user vào vi tri 0 và hien thi
		 */
		public static function CreateTable(objc : Object):void {
			Cocaro.isView = false ;
			AllowExit = true ;
			BasicFunction.RemoveHideLoading() ;
			Cocaro.mainStage_mc.gotoAndStop(5);
			//Cocaro.tf.appendText("=>CT:" + ) ;
			Cocaro.mainStage_mc.btnBackTable.addEventListener(MouseEvent.CLICK, clickExitTable) ;			
			Cocaro.mainStage_mc.Setting_btn.addEventListener(MouseEvent.CLICK, Setting_Click);
			Cocaro.mainStage_mc.Sound_btn.addEventListener(MouseEvent.CLICK, Sound_Click);
			ChatFunction.InitChat_InTable() ;
			if (Cocaro.isSound) {
				Cocaro.mainStage_mc.Sound_btn.gotoAndStop("active") ;
			} else {
				Cocaro.mainStage_mc.Sound_btn.gotoAndStop("inactive") ;
			}		/////////////////
			if (TimerInTable.running)
				TimerInTable.stop() ;
			TimerInTable = new Timer(1000) ;
			TimerInTable.repeatCount = 2 ;			
			TimerInTable.addEventListener(TimerEvent.TIMER, StartTimerInTable);			
			TimerInTable.start() ;
			Cocaro.mainStage_mc.Player1_mc.visible = false ;
			Cocaro.mainStage_mc.Player2_mc.visible = false ;			
			Cocaro.mainStage_mc.btnBackTable.visible = false ;
			//Cocaro.mainStage_mc.btn_Xinthua.visible = false ;
			Cocaro.mainStage_mc.btn_Cauhoa.visible = false ;
			//Cocaro.mainStage_mc.btn_Xindilai.visible = false ;
			Cocaro.mainStage_mc.Bangtyso_mc.visible = false ;	
			AddBtnReady() ;	
			//}
			var object = objc ;
			currTable = new ObjectTable() ;
			currTable.TableID = object.Id ;
			currTable.CurrentPlayer = 1 ;
			currTable.MarkUnit = object.MarkUnit ;
			currTable.TableName = object.Name ;
			currTable.TimePer = object.TimePer ;
			currTable.TotalTime = object.TotalTime ;
			currTable.Status = 0 ;
			Cc.log("11111");
			Cocaro.currPlayer.CurrTableID = object.Id ;
			Cocaro.currPlayer.IsCreateTable = true ;
			Cocaro.currPlayer.Ready = false ;
			Cocaro.currPlayer.Type = 1 ; //La nguoi san sang choi
			currTable.ArrPlayers = new Array() ;
			currTable.ArrPlayers.push(Cocaro.currPlayer) ;			
			Cc.log("11111");
			Cocaro.mainStage_mc.MarkUnit_txt.text = BasicFunction.numberFormat(currTable.MarkUnit, 0, false, true) + " \n Bàn:" + currTable.TableID;			
			currPosition = 0 ;
			//Cocaro.tf.appendText("=>" +currTable.ArrPlayers[0].TotalGames + "-" + currTable.ArrPlayers[0].WinGames) ;
			//{ Hien thi thong tin player
			Cc.log("11111");
			LoadData_Player(currTable.ArrPlayers[0].PlayerName, currTable.ArrPlayers[0].Mark, currTable.ArrPlayers[0].TinyURL, false, 
							currTable.ArrPlayers[0].TotalGames, currTable.ArrPlayers[0].WinGames, 1, currTable.ArrPlayers[0].VIP) ;
			//}
			var obj:Object = new Object();
			obj.evt = "rtable";
			Cocaro.mainSocket.sendData(JSON.encode(obj));			
		}
		/*********************************** View Table ***************************************************/
		public static function ViewTable(obje: Object):void { //User vao view Table
			BasicFunction.RemoveHideLoading() ;
			Cocaro.isView = true ;
			AllowExit = true ;
			//{ Cap nhat thong tin Player cua ban
			var object = obje;
			currTable = new ObjectTable() ;
			currTable.TableID = object.Id ;
			currTable.TableName = object.Name ;
			currTable.MarkUnit = object.MarkUnit ;
			Cocaro.currPlayer.CurrTableID = object.Id ;
			Cocaro.currPlayer.IsCreateTable = false ;
			Cocaro.currPlayer.Type = 0 ; //Chi la nguoi view
			currTable.ArrPlayers = new Array() ;
			var arrP : Array = object.ArrPlay ;			
			var item ;
			for (var i:int = 0; i < arrP.length; i++) 
			{
				item = arrP[i];
				var objPl : ObjectPlayer = new ObjectPlayer() ;
				objPl.PlayerID = item.Name ;
				objPl.PlayerName = item.Name ;
				objPl.Mark = item.Mark ;
				objPl.MarkLQ = item.LQ ;
				objPl.Ready = item.isStart ;
				objPl.TinyURL = item.TinyUrl ;
				objPl.VIP = item.VIP ;
				objPl.ArrChess = new Array() ;
				objPl.ArrChess_Movie = new Array() ;
				currTable.ArrPlayers.push(objPl) ;
			}
			currTable.CurrentPlayer = arrP.length ;
			currPosition = currTable.ArrPlayers.length - 1 ;	
			//}
			//tf.appendText("View img:\n") ;
			Cocaro.mainStage_mc.gotoAndStop(5);
			for (var j:int = 0; j < 10; j++) 
			{
				for (var k:int = 0; k < 9; k++) 
				{
					Cocaro.mainStage_mc["Checked" + j.toString() + k.toString() + "_mc"].visible = false ;
				}
			}			
			Cocaro.mainStage_mc.btnBackTable.addEventListener(MouseEvent.CLICK, clickExitTable) ;
			Cocaro.mainStage_mc.Setting_btn.addEventListener(MouseEvent.CLICK, Setting_Click);
			Cocaro.mainStage_mc.Sound_btn.addEventListener(MouseEvent.CLICK, Sound_Click);
			ChatFunction.InitChat_InTable() ;
			if (Cocaro.isSound) {
				Cocaro.mainStage_mc.Sound_btn.gotoAndStop("active") ;
			} else {
				Cocaro.mainStage_mc.Sound_btn.gotoAndStop("inactive") ;
			}
			//Khoi tao thoi gian hien thi nut Thoat ban			
			if (TimerInTable.running)
				TimerInTable.stop() ;
			TimerInTable = new Timer(1000) ;
			TimerInTable.repeatCount = 2 ;			
			TimerInTable.addEventListener(TimerEvent.TIMER, StartTimerInTable);			
			TimerInTable.start() ;			
			//Hien thi len giao dien Player
			Cocaro.mainStage_mc.MarkUnit_txt.text = BasicFunction.numberFormat(currTable.MarkUnit, 0, false, true) + "/n Bàn:" + currTable.TableID;
			Cocaro.mainStage_mc.Player1_mc.visible = false ;
			Cocaro.mainStage_mc.Player2_mc.visible = false ;			
			Cocaro.mainStage_mc.btnBackTable.visible = false ;
			//Cocaro.mainStage_mc.btn_Xinthua.visible = false ;
			Cocaro.mainStage_mc.btn_Cauhoa.visible = false ;
			//Cocaro.mainStage_mc.btn_Xindilai.visible = false ;
			Cocaro.mainStage_mc.Bangtyso_mc.visible = false ;
			//}
			//{ Thong tin Player 
			LoadData_Player(currTable.ArrPlayers[currTable.ArrPlayers.length - 1].PlayerName, currTable.ArrPlayers[currTable.ArrPlayers.length - 1].Mark
						, currTable.ArrPlayers[currTable.ArrPlayers.length - 1].TinyURL, false, currTable.ArrPlayers[currTable.ArrPlayers.length - 1].TotalGames, currTable.ArrPlayers[currTable.ArrPlayers.length - 1].WinGames, 1, currTable.ArrPlayers[currTable.ArrPlayers.length - 1].VIP) ;			
			//Thong tin cac Player con lai
			LoadData_Player(currTable.ArrPlayers[0].PlayerName, currTable.ArrPlayers[0].Mark, currTable.ArrPlayers[0].TinyURL, false, currTable.ArrPlayers[0].TotalGames, currTable.ArrPlayers[0].WinGames, 2, currTable.ArrPlayers[0].VIP) ;			
			//}			
		}
		/*
		 * Ham xu ly khi Player join vao ban dang choi
		 * ==> Khoi tao cac thong tin cua ban
		 * ==> Neu so user trong ban < 4 Add thong tin user vao vi tri 0 ==> Nguoc lai add thong tin user vao vi tri 5
		 * ==> Add cac thong tin ban va hien thi
		 */
		public static function JoinTable(obj : Object) : void { //Thang Player join ban
			MyWin = 0 ;
			OtherWin = 0 ;
			AllowExit = true ;
			BasicFunction.RemoveHideLoading() ;
			try {	
				if (Cocaro.isView) { 
					var mcRemove : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Chess")) ;
					while (mcRemove != null) {
						Cocaro.mainStage_mc.removeChild(mcRemove) ;
						mcRemove = MovieClip(Cocaro.mainStage_mc.getChildByName("Chess")) ;
					}
				} else {
					//{ Join ban tu phong choi
					Cocaro.mainStage_mc.gotoAndStop(5);
	
					Cocaro.mainStage_mc.btnBackTable.addEventListener(MouseEvent.CLICK, clickExitTable) ;
					Cocaro.mainStage_mc.Setting_btn.addEventListener(MouseEvent.CLICK, Setting_Click);
					Cocaro.mainStage_mc.Sound_btn.addEventListener(MouseEvent.CLICK, Sound_Click);
					ChatFunction.InitChat_InTable() ;
					if (Cocaro.isSound) {
						Cocaro.mainStage_mc.Sound_btn.gotoAndStop("active") ;
					} else {
						Cocaro.mainStage_mc.Sound_btn.gotoAndStop("inactive") ;
					}
					AddBtnReady() ;
					Cocaro.mainStage_mc.btnBackTable.visible = false ;
					//Cocaro.mainStage_mc.btn_Xinthua.visible = false ;
					Cocaro.mainStage_mc.btn_Cauhoa.visible = false ;
					//Cocaro.mainStage_mc.btn_Xindilai.visible = false ;
					Cocaro.mainStage_mc.Bangtyso_mc.visible = false ;
					if (TimerInTable.running)
						TimerInTable.stop() ;
					TimerInTable = new Timer(1000) ;
					TimerInTable.repeatCount = 2 ;			
					TimerInTable.addEventListener(TimerEvent.TIMER, StartTimerInTable);			
					TimerInTable.start() ;
					//}
				}
				//{ Cap nhat thong tin Player cua ban choi
				var object = obj;// JSON.decode(str) ;
				currTable = new ObjectTable() ;
				currTable.TableID = object.Id ;
				currTable.TableName = object.Name ;
				currTable.MarkUnit = object.MarkUnit ;
				currTable.TimePer = object.TimePer ;
				currTable.TotalTime = object.TotalTime ;			
				currTable.ArrPlayers = new Array() ;
				Cocaro.currPlayer.CurrTableID = object.Id ;
				Cocaro.currPlayer.IsCreateTable = false ;
				Cocaro.currPlayer.Type = 1 ; //La nguoi choi				
				var arrP : Array = object.ArrPlay ;			
				for (var i:int = 0; i < arrP.length; i++) 
				{
					var item = arrP[i];
					var objPl : ObjectPlayer = new ObjectPlayer() ;
					objPl.PlayerID = item.Name ;
					objPl.PlayerName = item.Name ;
					objPl.Mark = item.AG ;
					objPl.MarkLQ = item.LQ ;
					objPl.Ready = item.isStart ;			
					objPl.Type = 1 ;
					objPl.TotalGames = item.W + item.L ;
					objPl.WinGames = item.W ;
					objPl.TinyURL = item.TinyUrl ;
					objPl.VIP = item.VIP ;
					objPl.ArrChess = new Array() ;
					objPl.ArrChess_Movie = new Array() ;
					Cocaro.tf.appendText("==>" + item.Name + "-" + item.W + "-" + item.L + "-" + item.VIP) ;					
					currTable.ArrPlayers.push(objPl) ;
				}
				currTable.CurrentPlayer = arrP.length ;
				currPosition = currTable.ArrPlayers.length - 1 ;
				//}
				Cocaro.isView = false ;
				//{ Hien thi len giao dien Ban choi		
				Cocaro.mainStage_mc.MarkUnit_txt.text = BasicFunction.numberFormat(currTable.MarkUnit, 0, false, true) + "\n Bàn:" + currTable.TableID;
				Cocaro.mainStage_mc.Player2_mc.visible = false ;
				//}
				Cocaro.tf.appendText("==>abc") ;
				//{ View thong tin nguoi choi
				LoadData_Player(currTable.ArrPlayers[currTable.ArrPlayers.length - 1].PlayerName, currTable.ArrPlayers[currTable.ArrPlayers.length - 1].Mark
								, currTable.ArrPlayers[currTable.ArrPlayers.length - 1].TinyURL, false, currTable.ArrPlayers[currTable.ArrPlayers.length - 1].TotalGames, currTable.ArrPlayers[currTable.ArrPlayers.length - 1].WinGames,  1, currTable.ArrPlayers[currTable.ArrPlayers.length - 1].VIP) ;				
				LoadData_Player(currTable.ArrPlayers[0].PlayerName, currTable.ArrPlayers[0].Mark, currTable.ArrPlayers[0].TinyURL, currTable.ArrPlayers[0].Ready, currTable.ArrPlayers[0].TotalGames, currTable.ArrPlayers[0].WinGames, 2, currTable.ArrPlayers[0].VIP) ;
				//}
				if (TimerAutoExit.running) TimerAutoExit.stop() ;
				TimerAutoExit = new Timer(1000) ;
				TimerAutoExit.repeatCount = 20 ;
				TimerAutoExit.addEventListener(TimerEvent.TIMER, StartTimerAutoExit);				
				TimerAutoExit.start() ;
				Cocaro.mainStage_mc.txtTimeReady.visible = true ;
				Cocaro.mainStage_mc.txtTimeReady.text = "20" ;
				//tf.appendText("\njoin finish");
			} catch (ex : Error) {				
				//tf.appendText(ex.getStackTrace());
			}
		}
		/*
		 * Ham xu ly khi Player khac join ban cua minh dang choi
		 * ==> Them thong tin Player xem vao danh sach Player cua ban
		 * ==> Hien thi thong tin Player len giao dien neu van con 1 trong 4 cho
		 */ 
		public static function OtherJoinTable(objec : Object) : void { //Player khac join ban	
			MyWin = 0 ;
			OtherWin = 0 ;
			if (Cocaro.currPlayer.IsCreateTable)
				RemoveBtnInitGame() ;			
			var object = objec ;
			var objPl : ObjectPlayer = new ObjectPlayer() ;
			objPl.PlayerID = object.Name ;
			objPl.PlayerName = object.Name ;
			objPl.Mark = object.AG ;
			objPl.MarkLQ = object.LQ ;
			objPl.VIP = object.VIP ;
			objPl.Type = 1 ;
			objPl.TotalGames = object.W + object.L ;
			objPl.WinGames = object.W ;
			objPl.TinyURL = object.TinyUrl ;
			//tf.appendText("==>OtherBanURL:" + object.TinyUrl) ;
			objPl.ArrChess = new Array() ;
			objPl.ArrChess_Movie = new Array() ;
			var cs : int = -1;
			for (var i:int = 0; i < currTable.ArrPlayers.length; i++) 
			{
				if (Cocaro.currPlayer.PlayerName == currTable.ArrPlayers[i].PlayerName) {
					cs = i;
					break ;
				}				
			}	
			//{ Hien thi thong tin Player Join
			LoadData_Player(objPl.PlayerName, objPl.Mark, objPl.TinyURL, false, objPl.TotalGames, objPl.WinGames, 2, objPl.VIP) ;
			if (Cocaro.currPlayer.IsCreateTable) {
				Cocaro.mainStage_mc.Player2_mc.btnKick_mc.visible = true ;
				Cocaro.mainStage_mc.Player2_mc.btnKick_mc.addEventListener(MouseEvent.CLICK, KickPlayer2) ;
			}			
			//}
			Cocaro.mainStage_mc.Bangtyso_mc.Player2_txt.text = "0" ;
			Cocaro.mainStage_mc.Bangtyso_mc.Player1_txt.text = "0" ;
			currTable.ArrPlayers.push(objPl) ;
			currTable.CurrentPlayer++ ;			
		}
		/********************* Xu ly khi nhan duoc thong tin nguoi Ready **************************************/
		public static function ResetReadyInterface(stt : String) : void {
			Cocaro.mainStage_mc["Player" + stt + "_mc"].iconReady_mc.visible = true ;
			Cocaro.mainStage_mc["Player" + stt + "_mc"].MarkInGame_txt.text = "" ;
		}
		public static function clickReadyTable(evt : MouseEvent):void { //Kick Ready san sang de choi
			Cocaro.SoundKick() ;
			var obj:Object = new Object();
			obj.evt = "rtable";
			Cocaro.mainSocket.sendData(JSON.encode(obj));
		}
		public static function ReadyTable(object : Object) : void {
			//tf.appendText("\n==>Start");			
			var playerName : String = object.Name ;
			if (object.Name == Cocaro.currPlayer.PlayerName) {
				RemoveBtnRead() ;
				Cocaro.mainStage_mc.txtTimeReady.visible = false ;
				if (TimerAutoExit.running)
					TimerAutoExit.stop() ;
			}
			var isReady : Boolean = true;
			var cs : int = 0 ;
			for (var j:int = 0; j < currTable.ArrPlayers.length; j++) 
			{
				if (currTable.ArrPlayers[j].PlayerName == playerName) {
					cs = j ;
					currTable.ArrPlayers[j].Ready = true ;
					break ;
				}				
			}
			//tf.appendText("==>cs - currPosition:" + cs + "-" + currPosition + "-" + isReady) ;
			if ((currPosition - cs == 1) || (currPosition - cs == -1)) {
				Cocaro.mainStage_mc.Player2_mc.iconReady_mc.visible = true ;
			} else if (currPosition - cs == 0) {
				Cocaro.mainStage_mc.Player1_mc.iconReady_mc.visible = true ;
			}
			//Neu la Create Table thì hien thi nut Bat dau choi neu du ready
			if (Cocaro.currPlayer.IsCreateTable) {
				var totalReady : int = 0 ;
				for (var i:int = 0; i < currTable.ArrPlayers.length; i++) 
				{
					if (currTable.ArrPlayers[i].Ready) 
						totalReady++ ;				
				}
				//tf.appendText("==>Ready:" + totalReady) ;
				if ((totalReady == currTable.CurrentPlayer) && (totalReady > 1) && (currTable.Status != 1)) {
					AddBtnInitGame() ;
					currentAction = "" ;			
				}
			}
			//tf.appendText("\n==>Finished");
		}
		/******************** Xu ly khi nhan duoc lenh bat dau van choi **************************************/
		public static function CleanPlayerInterface(stt : String) : void {
			Cocaro.mainStage_mc["Player" + stt + "_mc"].btnKick_mc.visible = false ;
			Cocaro.mainStage_mc["Player" + stt + "_mc"].iconReady_mc.visible = false ;
			Cocaro.mainStage_mc["Player" + stt + "_mc"].txtMarkWin_txt.text = "";
			Cocaro.mainStage_mc["Player" + stt + "_mc"].MarkInGame_txt.text = "" ;
		}
		public static function ResetPlayerInterface(stt : String) : void {
			CleanPlayerInterface(stt) ;
			if (stt == "3")
				Cocaro.formatAvatar_MarkInGame.size = 14 ;
			else
				Cocaro.formatAvatar_MarkInGame.size = 12 ;
			Cocaro.mainStage_mc["Player" + stt + "_mc"].MarkInGame_txt.text = BasicFunction.numberFormat(currTable.MarkUnit, 0, false, true) ;
			Cocaro.mainStage_mc["Player" + stt + "_mc"].MarkInGame_txt.setTextFormat(Cocaro.formatAvatar_MarkInGame) ;
		}
		public static function ResetXY() : void {
			//{ Khoi tao Toa do vi tri cacm con bai
			positionCards = new Array() ;
			objPosition = new Object() ;
			objPosition.x0 = cardX[0]; 
			objPosition.y0 = cardY[0];
			positionCards.push(objPosition);//0 Bai tren tay player 1
			objPosition = new Object();
			objPosition.x0 = cardX[1]; 
			objPosition.y0 = cardY[1]; 
			positionCards.push(objPosition);//1 Bai tren tay player 2
			objPosition = new Object();
			objPosition.x0 = cardX[2]; 
			objPosition.y0 = cardY[2]; 
			positionCards.push(objPosition);//2 Bai tren tay player 3
			objPosition = new Object();
			objPosition.x0 = cardX[3]; 
			objPosition.y0 = cardY[3]; 
			positionCards.push(objPosition);//3 Bai tren tay player 4
			objPosition = new Object();
			objPosition.x0 = cardX[4]; 
			objPosition.y0 = cardY[4]; 
			positionCards.push(objPosition);//4 Bai tren tay player 5			
			objPosition = new Object();
			objPosition.x0 = cardX[5]; 
			objPosition.y0 = cardY[5]; 
			positionCards.push(objPosition);//4 Bai tren tay player 6
			objPosition = new Object();
			objPosition.x0 = cardX[6]; 
			objPosition.y0 = cardY[6]; 
			positionCards.push(objPosition);//4 Bai tren tay player 7
			objPosition = new Object();
			objPosition.x0 = cardX[7]; 
			objPosition.y0 = cardY[7]; 
			positionCards.push(objPosition);//4 Bai tren tay player 8
			//}
		}
		public static function ShowTime(value : int) { //Hien thi dong ho dem dung vi tri
			if (Cocaro.mainStage_mc.currentFrame == 5) {
				if (currPosition == currentTimerRun) {
					Cocaro.mainStage_mc.Player1_mc.txtTime_mc.text = value ;
					var second : int = parseInt(Cocaro.mainStage_mc.Player1_mc.Second_txt.text) ;
					var minute : int = parseInt(Cocaro.mainStage_mc.Player1_mc.Minute_txt.text) ;
					second = second - 1 ;
					if (second == -1) {
						second = 59 ;
						minute = minute - 1 ;
					}
					if (minute == -1) {
						minute = 0 ;
						second = 0 ;
					}
					if (second > 9)
						Cocaro.mainStage_mc.Player1_mc.Second_txt.text = second.toString() ;
					else 
						Cocaro.mainStage_mc.Player1_mc.Second_txt.text = "0" + second.toString() ;
					if (minute > 9)
						Cocaro.mainStage_mc.Player1_mc.Minute_txt.text = minute.toString() ;
					else 
						Cocaro.mainStage_mc.Player1_mc.Minute_txt.text = "0" + minute.toString() ;
				} else {
					Cocaro.mainStage_mc.Player2_mc.txtTime_mc.text = value ;
					var second : int = parseInt(Cocaro.mainStage_mc.Player2_mc.Second_txt.text) ;
					var minute : int = parseInt(Cocaro.mainStage_mc.Player2_mc.Minute_txt.text) ;
					second = second - 1 ;
					if (second == -1) {
						minute = minute - 1 ;
						second = 59 ;
					}
					if (minute == -1) {
						minute = 0 ;
						second = 0 ;
					}
					if (second > 9)
						Cocaro.mainStage_mc.Player2_mc.Second_txt.text = second.toString() ;
					else 
						Cocaro.mainStage_mc.Player2_mc.Second_txt.text = "0" + second.toString() ;
					if (minute > 9)
						Cocaro.mainStage_mc.Player2_mc.Minute_txt.text = minute.toString() ;
					else 
						Cocaro.mainStage_mc.Player2_mc.Minute_txt.text = "0" + minute.toString() ;
				}
			}
		}
		public static function StartTimeMain(evt:TimerEvent) : void { //Dem thoi gian cho moi thao tac	
			ShowTime(mainTimer.repeatCount - mainTimer.currentCount) ;
			if ((mainTimer.currentCount == mainTimer.repeatCount) && (Cocaro.mainStage_mc.currentFrame == 5)) {				
				if (currentTimerRun == currPosition) {
					
					
				}
			}			
		}		
		public static function InitTimeMain() {
			if (mainTimer.running)
				mainTimer.stop() ;
			mainTimer = new Timer(1000) ;
			mainTimer.repeatCount = 15 ;
			mainTimer.addEventListener(TimerEvent.TIMER, StartTimeMain);				
			mainTimer.start() ;
		}
		public static function clickStartTable(evt : MouseEvent):void {				
			Cocaro.SoundKick() ;
			var ob:Object = new Object();
			ob.evt = "sgame";
			////Cocaro.tf.appendText("==>Start:" + JSON.encode(ob)) ;
			Cocaro.mainSocket.sendData(JSON.encode(ob));
		}
		public static function StartSound() {
			if (Cocaro.isSound) {
				try 
				{
					if (startSound == null) {
						startSound = new Sound(new URLRequest(preURLSound + "StartGame.mp3")) ;
						startSound.addEventListener(Event.COMPLETE, PlaySound) ;
					} else 
						startSound.play() ;
					function PlaySound(event : Event) {
						startSound.play() ;
					}
				} catch (e:Error) { }
			}
		}

		public static function WinSound() {
			if (Cocaro.isSound) {
				try 
				{
					if (winSound == null) {
						winSound = new Sound(new URLRequest(preURLSound + "Win.mp3")) ;
						winSound.addEventListener(Event.COMPLETE, PlaySound) ;
					} else 
						winSound.play() ;
					function PlaySound(event : Event) {
						winSound.play() ;
					}
				} catch (e:Error) { }
			}
		}
		public static function LoseSound() {
			if (Cocaro.isSound) {
				try 
				{
					if (loseSound == null) {
						loseSound = new Sound(new URLRequest(preURLSound + "Lose.mp3")) ;
						loseSound.addEventListener(Event.COMPLETE, PlaySound) ;
					} else 
						loseSound.play() ;
					function PlaySound(event : Event) {
						loseSound.play() ;
					}
				} catch (e:Error) { }
			}
		}
		public static function RefreshSound() {
			if (Cocaro.isSound) {
				try 
				{
					if (refreshSound == null) {
						refreshSound = new Sound(new URLRequest(preURLSound + "RefreshFrientList.mp3")) ;
						refreshSound.addEventListener(Event.COMPLETE, PlaySound) ;
					} else 
						refreshSound.play() ;
					function PlaySound(event : Event) {
						refreshSound.play() ;
					}
				} catch (e:Error) { }
			}
		}
		
		public static function Init_Chess_Other(cs : int, color : int) : void {
			currTable.ArrPlayers[cs].ArrChess = new Array() ;
			//{Add Quan Tướng
			chess = new Chess() ;
			chess.Symbol = "Tg" ;
			chess.Stt = 1 ;
			chess.Hor = 0 ;
			chess.Ver = 4 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[cs].ArrChess.push(chess) ;			
			//}
			//{Add Quan Sỹ
			chess = new Chess() ;
			chess.Symbol = "S" ;
			chess.Stt = 1 ;
			chess.Hor = 0 ;
			chess.Ver = 3 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[cs].ArrChess.push(chess) ;
			chess = new Chess() ;
			chess.Symbol = "S" ;
			chess.Stt = 2 ;
			chess.Hor = 0 ;
			chess.Ver = 5 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[cs].ArrChess.push(chess) ;
			//}
			//{Add Quan Tinh
			chess = new Chess() ;
			chess.Symbol = "T" ;
			chess.Stt = 1 ;
			chess.Hor = 0 ;
			chess.Ver = 2 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[cs].ArrChess.push(chess) ;
			chess = new Chess() ;
			chess.Symbol = "T" ;
			chess.Stt = 2 ;
			chess.Hor = 0 ;
			chess.Ver = 6 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[cs].ArrChess.push(chess) ;
			//}
			//{Add Quan Ma
			chess = new Chess() ;
			chess.Symbol = "M" ;
			chess.Stt = 1 ;
			chess.Hor = 0 ;
			chess.Ver = 1 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[cs].ArrChess.push(chess) ;
			chess = new Chess() ;
			chess.Symbol = "M" ;
			chess.Stt = 2 ;
			chess.Hor = 0 ;
			chess.Ver = 7 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[cs].ArrChess.push(chess) ;
			//}
			//{Add Quan Xe
			var chess : Chess = new Chess() ;
			chess.Symbol = "X" ;
			chess.Stt = 1 ;
			chess.Hor = 0 ;
			chess.Ver = 0 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[cs].ArrChess.push(chess) ;
			chess = new Chess() ;
			chess.Symbol = "X" ;
			chess.Stt = 2 ;
			chess.Hor = 0 ;
			chess.Ver = 8 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[cs].ArrChess.push(chess) ;
			//}
			//{Add Quan Pháo
			chess = new Chess() ;
			chess.Symbol = "P" ;
			chess.Stt = 1 ;
			chess.Hor = 2 ;
			chess.Ver = 1 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[cs].ArrChess.push(chess) ;
			chess = new Chess() ;
			chess.Symbol = "P" ;
			chess.Stt = 2 ;
			chess.Hor = 2 ;
			chess.Ver = 7 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[cs].ArrChess.push(chess) ;
			//}			
			//{Add Quan Tốt
			chess = new Chess() ;
			chess.Symbol = "B" ;
			chess.Stt = 1 ;
			chess.Hor = 3 ;
			chess.Ver = 0 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[cs].ArrChess.push(chess) ;
			chess = new Chess() ;
			chess.Symbol = "B" ;
			chess.Stt = 2 ;
			chess.Hor = 3 ;
			chess.Ver = 2 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[cs].ArrChess.push(chess) ;
			chess = new Chess() ;
			chess.Symbol = "B" ;
			chess.Stt = 3 ;
			chess.Hor = 3 ;
			chess.Ver = 4 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[cs].ArrChess.push(chess) ;
			chess = new Chess() ;
			chess.Symbol = "B" ;
			chess.Stt = 4 ;
			chess.Hor = 3 ;
			chess.Ver = 6 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[cs].ArrChess.push(chess) ;
			chess = new Chess() ;
			chess.Symbol = "B" ;
			chess.Stt = 5 ;
			chess.Hor = 3 ;
			chess.Ver = 8 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[cs].ArrChess.push(chess) ;
			//}			
		}
		public static function Init_Chess(color : int) : void {
			currTable.ArrPlayers[currPosition].ArrChess = new Array() ;
			//{Add Quan Tướng
			var chess : Chess = new Chess() ;
			chess.Symbol = "Tg" ;
			chess.Stt = 1 ;
			chess.Hor = 9 ;
			chess.Ver = 4 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[currPosition].ArrChess.push(chess) ;			
			//}
			//{Add Quan Sỹ
			var chess : Chess = new Chess() ;
			chess.Symbol = "S" ;
			chess.Stt = 1 ;
			chess.Hor = 9 ;
			chess.Ver = 5 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[currPosition].ArrChess.push(chess) ;
			chess = new Chess() ;
			chess.Symbol = "S" ;
			chess.Stt = 2 ;
			chess.Hor = 9 ;
			chess.Ver = 3 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[currPosition].ArrChess.push(chess) ;
			//}
			//{Add Quan Tinh
			var chess : Chess = new Chess() ;
			chess.Symbol = "T" ;
			chess.Stt = 1 ;
			chess.Hor = 9 ;
			chess.Ver = 6 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[currPosition].ArrChess.push(chess) ;
			chess = new Chess() ;
			chess.Symbol = "T" ;
			chess.Stt = 2 ;
			chess.Hor = 9 ;
			chess.Ver = 2 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[currPosition].ArrChess.push(chess) ;
			//}
			//{Add Quan Ma
			var chess : Chess = new Chess() ;
			chess.Symbol = "M" ;
			chess.Stt = 1 ;
			chess.Hor = 9 ;
			chess.Ver = 7 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[currPosition].ArrChess.push(chess) ;
			chess = new Chess() ;
			chess.Symbol = "M" ;
			chess.Stt = 2 ;
			chess.Hor = 9 ;
			chess.Ver = 1 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[currPosition].ArrChess.push(chess) ;
			//}			
			//{Add Quan Xe
			var chess : Chess = new Chess() ;
			chess.Symbol = "X" ;
			chess.Stt = 1 ;
			chess.Hor = 9 ;
			chess.Ver = 8 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[currPosition].ArrChess.push(chess) ;
			chess = new Chess() ;
			chess.Symbol = "X" ;
			chess.Stt = 2 ;
			chess.Hor = 9 ;
			chess.Ver = 0 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[currPosition].ArrChess.push(chess) ;
			//}
			//{Add Quan Pháo
			var chess : Chess = new Chess() ;
			chess.Symbol = "P" ;
			chess.Stt = 1 ;
			chess.Hor = 7 ;
			chess.Ver = 7 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[currPosition].ArrChess.push(chess) ;
			chess = new Chess() ;
			chess.Symbol = "P" ;
			chess.Stt = 2 ;
			chess.Hor = 7 ;
			chess.Ver = 1 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[currPosition].ArrChess.push(chess) ;
			//}
			//{Add Quan Tốt
			var chess : Chess = new Chess() ;
			chess.Symbol = "B" ;
			chess.Stt = 1 ;
			chess.Hor = 6 ;
			chess.Ver = 8 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[currPosition].ArrChess.push(chess) ;
			chess = new Chess() ;
			chess.Symbol = "B" ;
			chess.Stt = 2 ;
			chess.Hor = 6 ;
			chess.Ver = 6 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[currPosition].ArrChess.push(chess) ;
			chess = new Chess() ;
			chess.Symbol = "B" ;
			chess.Stt = 3 ;
			chess.Hor = 6 ;
			chess.Ver = 4 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[currPosition].ArrChess.push(chess) ;
			chess = new Chess() ;
			chess.Symbol = "B" ;
			chess.Stt = 4 ;
			chess.Hor = 6 ;
			chess.Ver = 2 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[currPosition].ArrChess.push(chess) ;
			chess = new Chess() ;
			chess.Symbol = "B" ;
			chess.Stt = 5 ;
			chess.Hor = 6 ;
			chess.Ver = 0 ;
			chess.Status = 0 ;
			chess.Color = color ;
			currTable.ArrPlayers[currPosition].ArrChess.push(chess) ;
			//}
			
		}
		public static function Init_Graphic() {
			//{Khoi tao giao dien
			ArrCheck = new Array() ;
			for (var j:int = 0; j < 10; j++) 
			{
				var ArrHor : Array = new Array() ;
				for (var k:int = 0; k < 9; k++) 
				{
					Cocaro.mainStage_mc["Checked" + j.toString() + k.toString() + "_mc"].visible = false ;		
					Cocaro.mainStage_mc["Checked" + j.toString() + k.toString() + "_mc"].addEventListener(MouseEvent.CLICK, Checked_Click) ;
					Cocaro.mainStage_mc["Checked" + j.toString() + k.toString() + "_mc"].addEventListener(MouseEvent.MOUSE_OVER, Checked_Over) ;
					Cocaro.mainStage_mc["Checked" + j.toString() + k.toString() + "_mc"].addEventListener(MouseEvent.MOUSE_OUT, Checked_Out) ;
					ArrHor.push(0) ;
				}
				ArrCheck.push(ArrHor) ;
			}
			//}
			//{Hien thi Ban co
			for (var l:int = 0; l < currTable.ArrPlayers.length; l++) 
			{
				for (var i:int = 0; i < currTable.ArrPlayers[0].ArrChess.length; i++) 
				{
					var mc : MovieClip ;				
					if (currTable.ArrPlayers[l].ArrChess[i].Symbol == "Tg") {
						if (currTable.ArrPlayers[l].ArrChess[i].Color == 1)
							mc = MovieClip(new Tuong()) ;
						else 
							mc = MovieClip(new Tuong_Den()) ;
					} else if (currTable.ArrPlayers[l].ArrChess[i].Symbol == "X") {
						if (currTable.ArrPlayers[l].ArrChess[i].Color == 1)
							mc = MovieClip(new Xe()) ;
						else 
							mc = MovieClip(new Xe_Den()) ;
					} else if (currTable.ArrPlayers[l].ArrChess[i].Symbol == "M") {
						if (currTable.ArrPlayers[l].ArrChess[i].Color == 1)
							mc = MovieClip(new Ma()) ;
						else 
							mc = MovieClip(new Ma_Den()) ;
					} else if (currTable.ArrPlayers[l].ArrChess[i].Symbol == "T") {
						if (currTable.ArrPlayers[l].ArrChess[i].Color == 1)
							mc = MovieClip(new Tinh()) ;
						else 
							mc = MovieClip(new Tinh_Den()) ;
					} else if (currTable.ArrPlayers[l].ArrChess[i].Symbol == "S") {
						if (currTable.ArrPlayers[l].ArrChess[i].Color == 1)
							mc = MovieClip(new Sy()) ;
						else
							mc = MovieClip(new Sy_Den()) ;
					} else if (currTable.ArrPlayers[l].ArrChess[i].Symbol == "P") {
						if (currTable.ArrPlayers[l].ArrChess[i].Color == 1)
							mc = MovieClip(new Phao()) ;
						else
							mc = MovieClip(new Phao_Den()) ;
					} else if (currTable.ArrPlayers[l].ArrChess[i].Symbol == "B") {
						if (currTable.ArrPlayers[l].ArrChess[i].Color == 1)
							mc = MovieClip(new Tot()) ;					
						else
							mc = MovieClip(new Tot_Den()) ;
					}
					mc.name = "Chess" ;
					mc.x = xStart + currTable.ArrPlayers[l].ArrChess[i].Ver * xRange ;
					mc.y = yStart + currTable.ArrPlayers[l].ArrChess[i].Hor * yRange ;
					mc.gotoAndStop(1) ;
					ArrCheck[currTable.ArrPlayers[l].ArrChess[i].Hor][currTable.ArrPlayers[l].ArrChess[i].Ver] = currTable.ArrPlayers[l].ArrChess[i].Color ;
					new ChessController(mc, currTable.ArrPlayers[l].ArrChess[i]) ;
					currTable.ArrPlayers[l].ArrChess_Movie.push(mc) ;
					Cocaro.mainStage_mc.addChild(mc) ;
				}
			}			
			//}
		}
		public static function Clean_Frame5() {
			for (var i : int = 0 ; i < arrayTiles.length; i ++ ) {
				var char : MovieClip = MovieClip(arrayTiles[i].getChildByName("char"));
				char.gotoAndStop("blank");
			}
			var mcRemove : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Chess")) ;
			while (mcRemove != null) {
				Cocaro.mainStage_mc.removeChild(mcRemove) ;
				mcRemove = MovieClip(Cocaro.mainStage_mc.getChildByName("Chess")) ;
			}
			mcRemove = MovieClip(Cocaro.mainStage_mc.getChildByName("IconFinished")) ;
			while (mcRemove != null) {
				Cocaro.mainStage_mc.removeChild(mcRemove) ;
				mcRemove = MovieClip(Cocaro.mainStage_mc.getChildByName("IconFinished")) ;
			}
			Cocaro.mainStage_mc.Player2_mc.iconReady_mc.visible = false ;
			Cocaro.mainStage_mc.Player2_mc.btnKick_mc.visible = false ;
			Cocaro.mainStage_mc.Player2_mc.txtTime_mc.text = "" ;
			Cocaro.mainStage_mc.Player1_mc.iconReady_mc.visible = false ;
			Cocaro.mainStage_mc.Player1_mc.txtTime_mc.text = "" ;
		}
		/******************* Doi chu ban *********************************************************************/
		public static function ChangeTable(str : String) {
			//AS3Cay.tf.appendText("==>CC:" + str + "--" + currTable.ArrPlayers.length) ;
			for (var i:int = 0; i < currTable.ArrPlayers.length; i++) 
			{
				if (currTable.ArrPlayers[i].PlayerName == str) {
					currTable.ArrPlayers[i].IsCreateTable = true ;
				} else 
					currTable.ArrPlayers[i].IsCreateTable = false ;
			}
			if (Cocaro.currPlayer.PlayerName == str) {
				Cocaro.currPlayer.IsCreateTable = true ;
				Cocaro.mainStage_mc.Setting_btn.visible = true ;
				//Hien thi cac nut kick neu co
				if (currTable.CurrentPlayer > 1) {
					Cocaro.mainStage_mc.Player2_mc.btnKick_mc.visible = true ;
					Cocaro.mainStage_mc.Player2_mc.btnKick_mc.addEventListener(MouseEvent.CLICK, KickPlayer2) ;
				}
				//Check xem co hien thi nut bat dau chua
				var totalReady : int = 0 ;
				for (var i:int = 0; i < currTable.ArrPlayers.length; i++) 
				{
					if (currTable.ArrPlayers[i].Ready) 
						totalReady++ ;				
				}
				//AS3Cay.tf.appendText("==>TotalReady:" + currTable.CurrentPlayer + "-" + totalReady) ;
				if ((totalReady == currTable.CurrentPlayer) && (totalReady > 1) && (currTable.Status != 1)) {
					AddBtnInitGame() ;
					//if (CheckStartTable()) 
						//AS3Cay.mainStage_mc.btnInitGame.visible = true ;
				} else RemoveBtnInitGame() ; // AS3Cay.mainStage_mc.btnInitGame.visible = false ;
				for (var j:int = 0; j < currTable.ArrPlayers.length; j++) 
				{
					if ((currTable.ArrPlayers[j].PlayerName == Cocaro.currPlayer.PlayerName) && (currTable.ArrPlayers[j].Ready == false)) {
						var obj:Object = new Object();
						obj.evt = "rtable";
						Cocaro.mainSocket.sendData(JSON.encode(obj));
					}
				}
			}
		}
		/**
		 * draw tile in table
		 */
		public static function drawGameBoard(): void {
			arrayTiles = new Array;
			for (var i: int = 0 ; i < numTileX ; i++ ) {
				for (var j :int = 0 ; j < numTileY ; j++) {
					var tile_mc : MovieClip = MovieClip(new caro_tile_mc);
					tile_mc.x = i * tile_mc.width + marginL;
					tile_mc.y = j * tile_mc.height + marginT;
					tile_mc.H = j; tile_mc.V = i;
					tile_mc.justGo = false;
					arrayTiles.push(tile_mc);
					tile_mc.addEventListener(MouseEvent.CLICK , clickTile);
					Cocaro.mainStage_mc.caro_board_mc.addChild(tile_mc);
				}
			}
			function clickTile(evt : MouseEvent) {
				if (!clientEnable) return ;
				if (currTable.Status != 1) return;
				if (!turn) return;
				clientEnable = false;
				var obj : Object = new Object() ;
				obj.S = 0; obj.Stt = 1;
				obj.H = evt.target.H; obj.V = evt.target.V;
				obj.N = Cocaro.currPlayer.PlayerName;
				obj.evt = "playchess";
				Cocaro.mainSocket.sendData(JSON.encode(obj));
			}
		}
		public static function InitGame(strLC :String) : void {
			turn = (Cocaro.currPlayer.PlayerName == strLC) ? true: false;
			if (!Cocaro.isView) AllowExit = false ;
			//{An hien cac nut
			if (Cocaro.currPlayer.Type !=0) {
				Cocaro.mainStage_mc.btnBackTable.visible = false ;
				//Cocaro.mainStage_mc.btn_Xinthua.visible = true ;
				Cocaro.mainStage_mc.btn_Cauhoa.visible = true ;
				//Cocaro.mainStage_mc.btn_Xindilai.visible = true ;
				Cocaro.mainStage_mc.btn_Cauhoa.gotoAndStop(1) ;
				//Cocaro.mainStage_mc.btn_Xindilai.gotoAndStop(2) ;				
				//Cocaro.mainStage_mc.btn_Xinthua.gotoAndStop(2) ;	
				//Cocaro.mainStage_mc.btn_Xindilai.removeEventListener(MouseEvent.CLICK, clickXindilai) ;
				//Cocaro.mainStage_mc.btn_Xinthua.removeEventListener(MouseEvent.CLICK, clickXinthua) ;
				Cocaro.mainStage_mc.btn_Cauhoa.addEventListener(MouseEvent.CLICK, clickCauhoa) ;				
				Cocaro.mainStage_mc.Bangtyso_mc.visible = true ;
			}
			//tf.appendText("\r\n sgame => 2");
			Clean_Frame5() ;
			//}
			var i:Number;
			var j:Number;
			RemoveBtnInitGame() ;
			RemoveBtnRead() ;
			//tf.appendText("\r\n sgame => 3");
			//{Khoi tao lai van bai
			for (j = 0; j < currTable.ArrPlayers.length; j++) 
			{
				currTable.ArrPlayers[j].ArrChess = new Array() ;
				currTable.ArrPlayers[j].ArrChess_Movie = new Array() ;								
				currTable.ArrPlayers[j].LastObject = new Object() ;
			}
			currTable.Status = 1 ; 
			demCH = 0 ;
			demDL = 0 ;
			demPlay = 0 ;
			ArrOldPlay = new Array() ;
			ArrUpdateMoney = new Array() ;
			ArrExitTable = new Array() ;
			Cocaro.LastMarkTable = currTable.MarkUnit ;
			//}			
			if (currPosition == 0) {
				MyColor = 1 ;
				OtherColor = 2 ;
				csOther = 1 ;
			} else {
				MyColor = 2 ;
				OtherColor = 1 ;
				csOther = 0 ;
			}
			var userStart : String = strLC ;
			var csStart : int = 0 ;
			for (j = 0; j < currTable.ArrPlayers.length; j++) 
			{
				if (currTable.ArrPlayers[j].PlayerName == userStart) {
					csStart = j ;
					break ;
				}
			}
			//tf.appendText("\r\n sgame => 4");
			Init_Chess(MyColor) ;
			Init_Chess_Other(csOther, OtherColor) ;
			Init_Graphic() ;
			//tf.appendText("\r\n sgame => 5");
			mainTimer = new Timer(1000) ;
			mainTimer.repeatCount = currTable.TimePer ;//120 ;					
			mainTimer.addEventListener(TimerEvent.TIMER, StartTimeMain);		
			mainTimer.start() ;
			currentTimerRun = csStart ;
			if (currentTimerRun == currPosition) {
				Cocaro.mainStage_mc.Player1_mc.txtTime_mc.visible = true ;				
			} else {
				Cocaro.mainStage_mc.Player2_mc.txtTime_mc.visible = true ;				
			}
			Cocaro.mainStage_mc.Player1_mc.Minute_txt.text = currTable.TotalTime.toString() ;// "15" ;
			Cocaro.mainStage_mc.Player1_mc.Second_txt.text = "00" ;
			Cocaro.mainStage_mc.Player2_mc.Minute_txt.text = currTable.TotalTime.toString() ;//"15" ;
			Cocaro.mainStage_mc.Player2_mc.Second_txt.text = "00" ;
			if (currPosition == 0) {
				Cocaro.mainStage_mc.Bangtyso_mc.Player1_txt.text = MyWin.toString() ;
				Cocaro.mainStage_mc.Bangtyso_mc.Player2_txt.text = OtherWin.toString() ;
			} else {
				Cocaro.mainStage_mc.Bangtyso_mc.Player2_txt.text = MyWin.toString() ;
				Cocaro.mainStage_mc.Bangtyso_mc.Player1_txt.text = OtherWin.toString() ;
			}
			//tf.appendText("\r\n sgame => 6");
			StartSound() ;
			//tf.appendText("\r\n sgame finish");
		}
		public static function Checked_Click(evt :MouseEvent) {	
			if (currTable.Status != 1) return ;
			var name : String = evt.target.name;
			var hor : int = parseInt(name.charAt(7)) ;
			var ver : int = parseInt(name.charAt(8)) ;
			var csOld : int = 0 ;
			for (var j:int = 0; j < currTable.ArrPlayers[currPosition].ArrChess.length; j++) 
			{
				if ((currTable.ArrPlayers[currPosition].ArrChess[j].Symbol == chessSelected.Symbol) && (currTable.ArrPlayers[currPosition].ArrChess[j].Stt == chessSelected.Stt)) {
					csOld = j ;
					break ;
				}
			}
			if (!CheckPlay(hor, ver, chessSelected.Hor, chessSelected.Ver, chessSelected.Symbol, chessSelected.Stt, csOld)) {
				Cocaro.mainStage_mc.Chat_mc.InTable_txt.htmlText += "<font color=\"#FF0000\">Không được chiếu đi chiếu lại một nước </font>\n" ;
				Cocaro.mainStage_mc.Chat_mc.All_txt.htmlText += "<font color=\"#FF0000\">Không được chiếu đi chiếu lại một nước </font>\n" ;
				//BasicFunction.ShowAlert("Bạn đã chiếu đi chiếu lại nước này nhiều lần!") ;
				return ;
			}
			//Cocaro.tf.appendText("=>Play:" + ForgedChess_Play(hor, ver, chessSelected.Hor, chessSelected.Ver, csOld)) ;
			if (ForgedChess_Play(hor, ver, chessSelected.Hor, chessSelected.Ver, csOld)) {
				//Send Data 
				var obj : Object = new Object() ;
				obj.S = chessSelected.Symbol ;
				obj.Stt = chessSelected.Stt ;
				obj.H = hor ;
				obj.V = ver ;
				obj.N = Cocaro.currPlayer.PlayerName ;
				for (var i:int = 0; i < currTable.ArrPlayers[csOther].ArrChess.length; i++) 
				{
					if (currTable.ArrPlayers[csOther].ArrChess_Movie[i].currentFrame != 1)
						currTable.ArrPlayers[csOther].ArrChess_Movie[i].gotoAndStop(1) ;
				}
				obj.evt = "playchess";
				Cocaro.mainSocket.sendData(JSON.encode(obj));	
			}			
		}
		public static function Checked_Over(evt :MouseEvent) {	
			//var name : String = evt.target.name;
			//mainStage_mc[name].over_mc.visible = true ;
		}
		public static function Checked_Out(evt :MouseEvent) {	
			//var name : String = evt.target.name;
			//mainStage_mc[name].over_mc.visible = false ;
		}
		public static function Check_ChieuTuong(csCheck : int, csTuong : int) : Boolean {
			//tf.appendText("==>CheckCT") ;
			var isChieutuong : Boolean = false ;
			isChieutuong = Tg_Controller.CheckChieuTuong() ;
			if (isChieutuong) {
				var ob:Object = new Object();
				ob.event = "chieutuong";
				ob.Name = Cocaro.currPlayer.PlayerName;
				ob.evt = "commandsend";
				Cocaro.mainSocket.sendData(JSON.encode(ob));
				return true ;
			}
			return false ;
		}
		public static function DisPlayConfirm(title : String, content : String) : void {
			var popup : MovieClip = MovieClip(new Popup_Confirm()) ;
			popup.OK_btn.addEventListener(MouseEvent.CLICK, XacnhanTB_click) ;
			popup.NotOK_btn.addEventListener(MouseEvent.CLICK, HuyboTB_click) ;
			popup.Content_txt.text = content ;
			popup.Title_txt.text = title ;
			popup.name = "Confirm" ;
			popup.x = 0 ;
			popup.y = 0 ;
			Cocaro.mainStage_mc.addChild(popup) ;
		}
		public static function XacnhanTB_click(ect : MouseEvent) : void {
			var mcRemove : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Confirm"));
			while (mcRemove != null) {
				Cocaro.mainStage_mc.removeChild(mcRemove) ;
				mcRemove = MovieClip(Cocaro.mainStage_mc.getChildByName("Confirm"));	
			}
			if (currentAction == "XT") {
				var ob:Object = new Object();
				ob.evt = "finishwarning";
				Cocaro.mainSocket.sendData(JSON.encode(ob));
			}
			else if (currentAction == "CH") {
				var ob:Object = new Object();
				ob.evt = "cocauhoa";
				Cocaro.mainSocket.sendData(JSON.encode(ob));
			}
			else if (currentAction == "DL") {
				var ob:Object = new Object();
				ob.Name = Cocaro.currPlayer.PlayerName;
				ob.evt = "codilai";
				Cocaro.mainSocket.sendData(JSON.encode(ob));
			}
			currentAction = "" ;
		}
		public static function HuyboTB_click(ect : MouseEvent) : void {
			var mcRemove : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Confirm"));
			while (mcRemove != null) {
				Cocaro.mainStage_mc.removeChild(mcRemove) ;
				mcRemove = MovieClip(Cocaro.mainStage_mc.getChildByName("Confirm"));	
			}
			if (currentAction == "CH") {
				var ob:Object = new Object();
				ob.event = "khongcauhoa";
				ob.Name = Cocaro.currPlayer.PlayerName;
				ob.evt = "commandsend";
				Cocaro.mainSocket.sendData(JSON.encode(ob));
			}
			else if (currentAction == "DL") {
				var ob:Object = new Object();
				ob.event = "khongdilai";
				ob.Name = Cocaro.currPlayer.PlayerName;
				ob.evt = "commandsend";
				Cocaro.mainSocket.sendData(JSON.encode(ob));
			}
			currentAction = "" ;
		}
		public static function clickXinthua(evt : MouseEvent):void {
			//Cocaro.tf.appendText("=>xin thua:" + currentAction) ;
			if (currentAction != "") return ;
			DisPlayConfirm("Xin Thua", "Bạn có chắc chắn muốn xin thua?") ;
			currentAction = "XT" ;
		}
		/******************* Cau hoa ******************************/
		public static function Other_KhongCauhoa(str : String) : void {
			if (str == Cocaro.currPlayer.PlayerName) return ;
			currentAction = "TB" ;			
			if (demCH == maxCH) {
				Cocaro.mainStage_mc.btn_Cauhoa.gotoAndStop(2) ;
				Cocaro.mainStage_mc.btn_Cauhoa.removeEventListener(MouseEvent.CLICK, clickCauhoa) ;
			}
			BasicFunction.ShowAlert(str + " không đồng ý cầu hòa!") ;
			currentAction = "" ;
			//DisplayWarning(str + " không đồng ý cầu hòa!") ;
		}
		public static function Other_Cauhoa(str : String) : void {
			if (str == Cocaro.currPlayer.PlayerName) return ;
			DisPlayConfirm("Cầu hòa", "Bạn đồng ý cho " + str + " cầu hòa?") ;
			currentAction = "CH" ;
		}
		public static function clickCauhoa(evt : MouseEvent):void {
			if (currentAction != "") return ;
			demCH++ ;
			var ob:Object = new Object();
			ob.event = "cauhoa";
			ob.Name = Cocaro.currPlayer.PlayerName;
			ob.evt = "commandsend";
			Cocaro.mainSocket.sendData(JSON.encode(ob));
		}
		/***************** Xin di lai **************************/
		public static function Other_KhongDilai(str : String) : void {
			if (str == Cocaro.currPlayer.PlayerName) return ;			
			if (demDL == maxDL) {				
				Cocaro.mainStage_mc.btn_Xindilai.gotoAndStop(2) ;
				Cocaro.mainStage_mc.btn_Xindilai.removeEventListener(MouseEvent.CLICK, clickXindilai) ;
			}
			//currentAction = "TB" ;
			currentAction = "" ;
			BasicFunction.ShowAlert(str + " không đồng ý cho đi lại!") ;
		}
		public static function Other_Xindilai(str : String) : void {
			//Cocaro.tf.appendText("=>Dilai:" + str) ;
			if (str == Cocaro.currPlayer.PlayerName) return ;
			DisPlayConfirm("Đi lại", "Bạn đồng ý cho " + str + " đi lại?") ;
			currentAction = "DL" ;
		}
		public static function clickXindilai(evt : MouseEvent):void {
			if (currentAction != "") return ;
			demDL++ ;
			var ob:Object = new Object();
			ob.event = "dilai";
			ob.Name = Cocaro.currPlayer.PlayerName;
			ob.evt = "commandsend";
			Cocaro.mainSocket.sendData(JSON.encode(ob));
			//Cocaro.tf.appendText("=>Click xin di lai:" + JSON.encode(ob)) ;
		}
		
		public static function Return(objLast : Object, csReturn : int, csTake : int, colorReturn : int , colorTake : int) : void {
			Cocaro.mainStage_mc.btn_Xindilai.gotoAndStop(2) ;
			Cocaro.mainStage_mc.btn_Xindilai.removeEventListener(MouseEvent.CLICK, clickXindilai) ;
			//tf.appendText("==> Return 1:" + objLast.NewH + "-" + objLast.NewV + "*" + objLast.OldH + "-" + objLast.OldV) ;
			//tf.appendText("==> S:" + objLast.S + "-" + objLast.Stt) ;
			//tf.appendText("==> Other:" + objLast.OtherS + "-" + objLast.OtherStt + "-" + objLast.OtherH + "-" + objLast.OtherV) ;
			ArrCheck[objLast.NewH][objLast.NewV] = 0 ;
			ArrCheck[objLast.OldH][objLast.OldV] = colorReturn ;
			for (var i:int = 0; i < currTable.ArrPlayers[csReturn].ArrChess.length; i++) 
			{
				if ((currTable.ArrPlayers[csReturn].ArrChess[i].Symbol == objLast.S) && (currTable.ArrPlayers[csReturn].ArrChess[i].Stt == objLast.Stt)) {
					currTable.ArrPlayers[csReturn].ArrChess[i].Hor = objLast.OldH ;
					currTable.ArrPlayers[csReturn].ArrChess[i].Ver = objLast.OldV ;					
					moveObj(currTable.ArrPlayers[csReturn].ArrChess_Movie[i], Cocaro.mainStage_mc["Checked" + objLast.OldH.toString() + objLast.OldV.toString() + "_mc"].x
						, Cocaro.mainStage_mc["Checked" + objLast.OldH.toString() + objLast.OldV.toString() + "_mc"].y) ;
					break ;
				}
			}
			//tf.appendText("==> Return Take") ;
			//{Neu nuoc truoc la nuoc an ==> Khoi phuc lai quan co
			if (objLast.OtherS != "") {
				var obj : Chess = new Chess() ;
				obj.Symbol = objLast.OtherS ;
				obj.Stt = objLast.OtherStt ;
				obj.Hor = objLast.OtherH ;
				obj.Ver = objLast.OtherV ;
				//if (csTake == currPosition) {
					//obj.Hor = 9 - obj.Hor ;
					//obj.Ver = 8 - obj.Ver ;
				//}
				obj.Status = 0 ;
				obj.Color = objLast.Color 
				ArrCheck[obj.Hor][obj.Ver] = colorTake ;
				currTable.ArrPlayers[csTake].ArrChess.push(obj) ;
				var mc : MovieClip ;				
				if (obj.Symbol == "Tg") {
					if (obj.Color == 1)
						mc = MovieClip(new Tuong()) ;
					else 
						mc = MovieClip(new Tuong_Den()) ;
				} else if (obj.Symbol == "X") {
					if (obj.Color == 1)
						mc = MovieClip(new Xe()) ;
					else 
						mc = MovieClip(new Xe_Den()) ;
				} else if (obj.Symbol == "M") {
					if (obj.Color == 1)
						mc = MovieClip(new Ma()) ;
					else 
						mc = MovieClip(new Ma_Den()) ;
				} else if (obj.Symbol == "T") {
					if (obj.Color == 1)
						mc = MovieClip(new Tinh()) ;
					else 
						mc = MovieClip(new Tinh_Den()) ;
				} else if (obj.Symbol == "S") {
					if (obj.Color == 1)
						mc = MovieClip(new Sy()) ;
					else
						mc = MovieClip(new Sy_Den()) ;
				} else if (obj.Symbol == "P") {
					if (obj.Color == 1)
						mc = MovieClip(new Phao()) ;
					else
						mc = MovieClip(new Phao_Den()) ;
				} else if (obj.Symbol == "B") {
					if (obj.Color == 1)
						mc = MovieClip(new Tot()) ;					
					else
						mc = MovieClip(new Tot_Den()) ;
				}
				mc.name = "Chess" ;
				mc.x = xStart + obj.Ver * xRange ;
				mc.y = yStart + obj.Hor * yRange ;
				mc.gotoAndStop(1) ;					
				new ChessController(mc, obj) ;
				currTable.ArrPlayers[csTake].ArrChess_Movie.push(mc) ;
				Cocaro.mainStage_mc.addChild(mc) ;						
			}
			//}
		}

		public static function Dilai(str : String) : void {
			//tf.appendText("DL:" + str) ;
			if (str == Cocaro.currPlayer.PlayerName) {
				if (currentTimerRun == currPosition) { //Hoan lai nuoc co cua ca 2
					Return(currTable.ArrPlayers[csOther].LastObject, csOther, currPosition, OtherColor, MyColor) ;
				} else { //Chi hoan lai nuoc co cua minh
					Return(currTable.ArrPlayers[currPosition].LastObject, currPosition, csOther, MyColor, OtherColor) ;
					Return(currTable.ArrPlayers[csOther].LastObject, csOther, currPosition, OtherColor, MyColor) ;					
				}
			} else {
				if (currentTimerRun == currPosition) {
					Return(currTable.ArrPlayers[csOther].LastObject, csOther, currPosition, OtherColor, MyColor) ;
					Return(currTable.ArrPlayers[currPosition].LastObject, currPosition, csOther, MyColor, OtherColor) ;					
				} else {
					Return(currTable.ArrPlayers[currPosition].LastObject, currPosition, csOther, MyColor, OtherColor) ;
				}
			}
			//tf.appendText("==>Chuyen Timer") ;
			mainTimer.stop() ;
			if (str == Cocaro.currPlayer.PlayerName)
				currentTimerRun = csOther ;
			else currentTimerRun = currPosition ;	
			mainTimer = new Timer(1000) ;
			mainTimer.repeatCount = currTable.TimePer ;// 120 ;					
			mainTimer.addEventListener(TimerEvent.TIMER, StartTimeMain);		
			mainTimer.start() ;
			if (currentTimerRun == currPosition) {
				Cocaro.mainStage_mc.Player1_mc.txtTime_mc.visible = true ;
				Cocaro.mainStage_mc.Player2_mc.txtTime_mc.visible = false ;
			} else {
				Cocaro.mainStage_mc.Player1_mc.txtTime_mc.visible = false ;
				Cocaro.mainStage_mc.Player2_mc.txtTime_mc.visible = true ;
			}	
			currentAction == "" ;
		}
		public static function Warning(str : String) : void {
			if (str != Cocaro.currPlayer.PlayerName) return ;
			currentAction = "TB" ;
			BasicFunction.ShowAlert("Bạn không được chiếu quá 4 nước liên tục!") ;
		}		
		public static function ChieuTuong(str : String) {
			//tf.appendText("CT:" + str) ;
			//if (currPosition == 0) {
				if (str == Cocaro.currPlayer.PlayerName) {
					for (var i:int = 0; i < currTable.ArrPlayers[csOther].ArrChess.length; i++) 
					{
						if (currTable.ArrPlayers[csOther].ArrChess[i].Symbol == "Tg") {
							currTable.ArrPlayers[csOther].ArrChess_Movie[i].gotoAndStop(3) ;						
							currTable.ArrPlayers[csOther].ArrChess_Movie[i].Xet_mc.rotation = 180 ;
							currTable.ArrPlayers[csOther].ArrChess_Movie[i].Xet_mc.x = 0 ;
							currTable.ArrPlayers[csOther].ArrChess_Movie[i].Xet_mc.y = 15 ;
						}					
					}
				} else {
					for (var i:int = 0; i < currTable.ArrPlayers[currPosition].ArrChess.length; i++) 
					{
						if (currTable.ArrPlayers[currPosition].ArrChess[i].Symbol == "Tg") {
							currTable.ArrPlayers[currPosition].ArrChess_Movie[i].gotoAndStop(3) ;						
							currTable.ArrPlayers[currPosition].ArrChess_Movie[i].Xet_mc.rotation = 0 ;
							currTable.ArrPlayers[currPosition].ArrChess_Movie[i].Xet_mc.x = 0 ;
							currTable.ArrPlayers[currPosition].ArrChess_Movie[i].Xet_mc.y = -20 ;
						}					
					}
				}
			//} else {
				//if (str == currPlayer.PlayerName) {
					//for (var i:int = 0; i < currTable.ArrPlayers[csOther].ArrChess.length; i++) 
					//{
						//if (currTable.ArrPlayers[csOther].ArrChess[i].Symbol == "Tg") {
							//currTable.ArrPlayers[csOther].ArrChess_Movie[i].gotoAndStop(3) ;						
							//currTable.ArrPlayers[csOther].ArrChess_Movie[i].Xet_mc.rotation = 180 ;
							//currTable.ArrPlayers[csOther].ArrChess_Movie[i].Xet_mc.x = 0 ;
							//currTable.ArrPlayers[csOther].ArrChess_Movie[i].Xet_mc.y = 15 ;
						//}					
					//}
				//} else {
					//for (var i:int = 0; i < currTable.ArrPlayers[currPosition].ArrChess.length; i++) 
					//{
						//if (currTable.ArrPlayers[currPosition].ArrChess[i].Symbol == "Tg") {
							//currTable.ArrPlayers[currPosition].ArrChess_Movie[i].gotoAndStop(3) ;						
							//currTable.ArrPlayers[currPosition].ArrChess_Movie[i].Xet_mc.rotation = 0 ;
							//currTable.ArrPlayers[currPosition].ArrChess_Movie[i].Xet_mc.x = 0 ;
							//currTable.ArrPlayers[currPosition].ArrChess_Movie[i].Xet_mc.y = -20 ;
						//}					
					//}
				//}
			//}			
			//var mc : MovieClip = MovieClip(new Chieutuong()) ;
			//mc.rotation
			//mc.name = "Chieutuong" ;
			//mc.x = 165 ;
			//mc.y = 270 ;
			//mainStage_mc.addChild(mc) ;			
		}
		/**
		 * 
		 * @param	obj
		 */
		public static function caroOtherPlay(obj: Object): void {
			try {
				var str : String = (typeTile) ? "char_o" : "char_x";
				var tile: MovieClip = getTileAtPosition(obj.H, obj.V);
				setTileJustGo(tile);
				Cocaro.mainStage_mc.caro_board_mc.setChildIndex(tile, arrayTiles.length - 1);
				var char : MovieClip = MovieClip(tile.getChildByName("char"));
				char.gotoAndPlay(str);
				clientEnable = true;
				if (checkFinish()) {
					Cc.log("finish");
				}
				turn = !turn;
				typeTile = !typeTile;
			}catch(e: Error){Cc.log(e)}
		}
		/**
		 * 
		 * @param	h
		 * @param	v
		 * @return
		 */
		private static function getTileAtPosition(h : int, v : int): MovieClip {
			for (var i: int = 0 ; i < arrayTiles.length ; i++) {
				if (arrayTiles[i].H == h && arrayTiles[i].V == v)
					return arrayTiles[i];
			}
			return null;
		}
		/**
		 * 
		 * @return
		 */
		private static function checkFinish(): Boolean {
			if (!turn) return false;
			return false;
		}
		/**
		 * 
		 * @param	tile_mc
		 * @param	direction
		 * @return
		 */
		private static function getArrayNext(tile_mc : MovieClip, direction: String) : Array {
			var arr : Array = new Array;
			var tile_next = getTileNext(tile_mc, direction);
			while (tile_next != null) {
				arr.push(tile_next);
				tile_next = getTileNext(tile_next, direction);
			}
			return arr;
		}
		
		/**
		 * 
		 * @param	tile_mc
		 * @param	direction
		 * @return
		 */
		private static function getTileNext(tile_mc : MovieClip , direction: String):MovieClip {
			var tile_next_mc : MovieClip;
			switch(direction) {
				case "B" : 
					tile_next_mc = getTileAtPosition(tile_mc.H, tile_mc.V - 1);
					break;
				case "N" : 
					tile_next_mc = getTileAtPosition(tile_mc.H, tile_mc.V + 1);
					break;
				case "D" : 
					tile_next_mc = getTileAtPosition(tile_mc.H + 1, tile_mc.V);
					break;
				case "T" : 
					tile_next_mc = getTileAtPosition(tile_mc.H - 1, tile_mc.V);
					break;
				case "DB" : 
					tile_next_mc = getTileAtPosition(tile_mc.H + 1, tile_mc.V - 1);
					break;
				case "DN" : 
					tile_next_mc = getTileAtPosition(tile_mc.H + 1, tile_mc.V + 1);
					break;
				case "TB" : 
					tile_next_mc = getTileAtPosition(tile_mc.H - 1, tile_mc.V - 1);
					break;
				default : 
					tile_next_mc = getTileAtPosition(tile_mc.H - 1, tile_mc.V + 1);
					break;
			}
			return tile_next_mc;
		}
		/**
		 * 
		 * @return
		 */
		private static function getTileJustGo() : MovieClip() {
			for (var i : int = 0 ; i < arrayTiles.length ; i ++ ) {
				if (arrayTiles[i].justGo) return arrayTiles[i];
			}
			return null;
		}
		
		/**
		 * 
		 * @param	tile
		 */
		private static function setTileJustGo(tile: MovieClip): void {
			clearTileJustGo();
			tile.justGo = true;	
		}
		/**
		 * 
		 */
		private static function clearTileJustGo(): void {
			for (var i : int = 0 ; i < arrayTiles.length ; i ++ ) {
				if (arrayTiles[i].justGo) {
					arrayTiles[i].justGo = false;
					break;
				}
			}
		}
		public static function Other_Play(objec : Object) {
			//tf.appendText("==>Nhan:") ;
			var mcRemove : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Chieutuong")) ;
			while (mcRemove != null) {
				Cocaro.mainStage_mc.removeChild(mcRemove) ;
				mcRemove = MovieClip(Cocaro.mainStage_mc.getChildByName("Chieutuong")) ;
			}
			var obj : Object = objec;// JSON.decode(str) ;
			var cs : int = 0 ;
			if (obj.N == Cocaro.currPlayer.PlayerName) cs = currPosition ;
			else cs = csOther ;
			//{Chuyen hien thi cac quan co ve Frame 2
			for (var o:int = 0; o < currTable.ArrPlayers.length; o++) 
			{
				for (var p:int = 0; p < currTable.ArrPlayers[o].ArrChess.length; p++) 
				{
					if (currTable.ArrPlayers[o].ArrChess_Movie[p].currentFrame != 1)
						currTable.ArrPlayers[o].ArrChess_Movie[p].gotoAndStop(1) ;					
				}				
			}
			//}
			var t : Boolean = true ;
			demPlay = demPlay + 1 ;
			var objOld : Object = new Object() ;
			objOld.S = obj.S ;
			objOld.Stt = obj.Stt ;
			objOld.Name = obj.N ;
			objOld.Chieu = false ;
			var objLast : Object = new Object() ;
			objLast.S = obj.S ;
			objLast.Stt = obj.Stt ;	
			if (cs == currPosition) {
				if ((demDL < maxDL) && (demPlay >= minVisibleDL)) {
					Cocaro.mainStage_mc.btn_Xindilai.gotoAndStop(1) ;
					Cocaro.mainStage_mc.btn_Xindilai.addEventListener(MouseEvent.CLICK, clickXindilai) ;
				}
				objLast.NewH = obj.H ;
				objLast.NewV = obj.V ;
				objOld.H = obj.H ;
				objOld.V = obj.V ;
				for (var i:int = 0; i < currTable.ArrPlayers[cs].ArrChess.length; i++) 
				{
					if ((currTable.ArrPlayers[cs].ArrChess[i].Symbol == obj.S) && (currTable.ArrPlayers[cs].ArrChess[i].Stt == obj.Stt)) {
						ArrCheck[currTable.ArrPlayers[cs].ArrChess[i].Hor][currTable.ArrPlayers[cs].ArrChess[i].Ver] = 0 ;
						objLast.OldH = currTable.ArrPlayers[cs].ArrChess[i].Hor ;
						objLast.OldV = currTable.ArrPlayers[cs].ArrChess[i].Ver ;
						objLast.OtherS = "" ;
						objLast.OtherStt = "" ;
						objLast.OtherH = 0 ;
						objLast.OtherV = 0 ;
						objLast.Color = 0 ;
						//{Check xem co quan doi phuong o toa do moi
						for (var l:int = 0; l < currTable.ArrPlayers[csOther].ArrChess.length; l++) 
						{
							if ((currTable.ArrPlayers[csOther].ArrChess[l].Hor == obj.H) && (currTable.ArrPlayers[csOther].ArrChess[l].Ver == obj.V)) { //An
								t = false ;
								if (currTable.ArrPlayers[csOther].ArrChess[l].Symbol == "Tg") {
									var ob:Object = new Object();
									ob.evt = "finishgame";
									Cocaro.mainSocket.sendData(JSON.encode(ob));
								}
								objLast.OtherS = currTable.ArrPlayers[csOther].ArrChess[l].Symbol ;
								objLast.OtherStt = currTable.ArrPlayers[csOther].ArrChess[l].Stt ;
								objLast.OtherH = currTable.ArrPlayers[csOther].ArrChess[l].Hor ;
								objLast.OtherV = currTable.ArrPlayers[csOther].ArrChess[l].Ver ;
								objLast.Color = OtherColor ;
								ArrCheck[obj.H][obj.V] = 0 ;								
								Cocaro.mainStage_mc.removeChild(currTable.ArrPlayers[csOther].ArrChess_Movie[l]) ;
								currTable.ArrPlayers[csOther].ArrChess.splice(l, 1) ;
								currTable.ArrPlayers[csOther].ArrChess_Movie.splice(l, 1) ;
														
								break ;
							}							
						}
						//}
						//{Update toa do moi
						ArrCheck[obj.H][obj.V] = MyColor ;
						currTable.ArrPlayers[cs].ArrChess[i].Hor = obj.H ;
						currTable.ArrPlayers[cs].ArrChess[i].Ver = obj.V ;
						moveObj(currTable.ArrPlayers[cs].ArrChess_Movie[i], Cocaro.mainStage_mc["Checked" + obj.H.toString() + obj.V.toString() + "_mc"].x, Cocaro.mainStage_mc["Checked" + obj.H.toString() + obj.V.toString() + "_mc"].y) ;
						currTable.ArrPlayers[cs].ArrChess_Movie[i].gotoAndStop(2) ;
						//}
						//{ Khoi tao lai
						for (var j:int = 0; j < 10; j++) 
						{
							for (var k:int = 0; k < 9; k++) 
							{
								Cocaro.mainStage_mc["Checked" + j.toString() + k.toString() + "_mc"].visible = false ;
							}
						}
						//}
						//{Check chieu tuong
						if (Check_ChieuTuong(currPosition, csOther)) {
							objOld.Chieu = true ;
							currTable.CountChieu = currTable.CountChieu + 1 ;							
						} else {
							currTable.CountChieu = 0 ;
						}
						break ;
					}
				}
				currTable.ArrPlayers[currPosition].LastObject = objLast ;
				if (CheckFinishGame()) {
					var ob:Object = new Object();
					ob.evt = "finishgame";
					Cocaro.mainSocket.sendData(JSON.encode(ob));
				}				
			} else {
				obj.H = 9 - obj.H ;
				obj.V = 8 - obj.V ;
				objLast.NewH = obj.H ;
				objLast.NewV = obj.V ;
				objOld.H = obj.H ;
				objOld.V = obj.V ;
				//tf.appendText("==>New:" + obj.H + "-" + obj.V + "-" + currTable.ArrPlayers[currPosition].ArrChess.length) ;
				for (var m:int = 0; m < currTable.ArrPlayers[cs].ArrChess.length; m++) 
				{
					if ((currTable.ArrPlayers[cs].ArrChess[m].Symbol == obj.S) && (currTable.ArrPlayers[cs].ArrChess[m].Stt == obj.Stt)) {
						//tf.appendText("True:" + currTable.ArrPlayers[cs].ArrChess[m].Hor + "-" + currTable.ArrPlayers[cs].ArrChess[m].Ver) ;
						ArrCheck[currTable.ArrPlayers[cs].ArrChess[m].Hor][currTable.ArrPlayers[cs].ArrChess[m].Ver] = 0 ;
						objLast.OldH = currTable.ArrPlayers[cs].ArrChess[m].Hor ;
						objLast.OldV = currTable.ArrPlayers[cs].ArrChess[m].Ver ;
						objLast.OtherS = "" ;
						objLast.OtherStt = "" ;
						objLast.OtherH = 0 ;
						objLast.OtherV = 0 ;
						objLast.Color = 0 ;
						//{Check xem co quan co cua minh o toa do moi
						for (var n:int = 0; n < currTable.ArrPlayers[currPosition].ArrChess.length; n++) 
						{
							if ((currTable.ArrPlayers[currPosition].ArrChess[n].Hor == obj.H) && (currTable.ArrPlayers[currPosition].ArrChess[n].Ver == obj.V)) { //An
								t = false ;								
								objLast.OtherS = currTable.ArrPlayers[currPosition].ArrChess[n].Symbol ;
								objLast.OtherStt = currTable.ArrPlayers[currPosition].ArrChess[n].Stt ;
								objLast.OtherH = currTable.ArrPlayers[currPosition].ArrChess[n].Hor ;
								objLast.OtherV = currTable.ArrPlayers[currPosition].ArrChess[n].Ver ;
								objLast.Color = MyColor ;
								ArrCheck[obj.H][obj.V] = 0 ;
								Cocaro.mainStage_mc.removeChild(currTable.ArrPlayers[currPosition].ArrChess_Movie[n]) ;
								currTable.ArrPlayers[currPosition].ArrChess.splice(n, 1) ;
								currTable.ArrPlayers[currPosition].ArrChess_Movie.splice(n, 1) ;
								
								break ;
							}							
						}
						//}
						//tf.appendText("==> Update") ;
						//{Update toa do moi
						ArrCheck[obj.H][obj.V] = OtherColor ;
						currTable.ArrPlayers[cs].ArrChess[m].Hor = obj.H ;
						currTable.ArrPlayers[cs].ArrChess[m].Ver = obj.V ;
						moveObj(currTable.ArrPlayers[cs].ArrChess_Movie[m], Cocaro.mainStage_mc["Checked" + obj.H.toString() + obj.V.toString() + "_mc"].x, Cocaro.mainStage_mc["Checked" + obj.H.toString() + obj.V.toString() + "_mc"].y) ;
						currTable.ArrPlayers[cs].ArrChess_Movie[m].gotoAndStop(2) ;						
						//}
						//{Check Lo mat tuong
						//if (Tg_Controller.Check_LomatTuong()) {
							//var ob:Object = new Object();
							//ob.evt = "finishgame";
							//Cocaro.mainSocket.sendData(JSON.encode(ob));
						//}						
						//}
						break ;
					}
				}
				currTable.ArrPlayers[csOther].LastObject = objLast ;
			}
			ArrOldPlay.push(objOld) ;
			if (t)
				RefreshSound() ;
			//{ Khoi tao lai thoi gian
			if (mainTimer.running)
				mainTimer.stop() ;
			mainTimer = new Timer(1000) ;
			mainTimer.repeatCount = currTable.TimePer ; //120					
			mainTimer.addEventListener(TimerEvent.TIMER, StartTimeMain);		
			mainTimer.start() ;
			if (cs == currPosition) currentTimerRun = csOther ;
			else currentTimerRun = currPosition ;
			if (currentTimerRun == currPosition) {
				Cocaro.mainStage_mc.Player1_mc.txtTime_mc.visible = true ;
				Cocaro.mainStage_mc.Player2_mc.txtTime_mc.visible = false ;
				//Cocaro.mainStage_mc.btn_Xinthua.gotoAndStop(1) ;
				//Cocaro.mainStage_mc.btn_Xinthua.addEventListener(MouseEvent.CLICK, clickXinthua) ;								
			} else {
				Cocaro.mainStage_mc.Player1_mc.txtTime_mc.visible = false ;
				Cocaro.mainStage_mc.Player2_mc.txtTime_mc.visible = true ;
				//Cocaro.mainStage_mc.btn_Xinthua.gotoAndStop(2) ;
				//Cocaro.mainStage_mc.btn_Xinthua.removeEventListener(MouseEvent.CLICK, clickXinthua) ;
			}
			//}
			//tf.appendText("==>Finish") ;
		}
		// Hàm tạo chuyển động cho movie đến vị trí mới, thời gian 1s
		public static function moveObj(_mc: MovieClip, xNew:Number, yNew:Number)	{				
			TweenMax.to(_mc, 0.8, { x:xNew, y:yNew, ease:Expo.easeOut, onComplete:Latbai() } );
			function Latbai()
			{
				
			}
		}
		/******************************************* Finish Game *********************************************/
		public static function FinishedTimer() {
			if (finishTimer.running)
				finishTimer.stop() ;
			finishTimer = new Timer(1000) ;
			finishTimer.repeatCount = 5 ;
			finishTimer.addEventListener(TimerEvent.TIMER, StartFinishedTimer);				
			finishTimer.start() ;
		}
		public static function StartFinishedTimer(evt:TimerEvent) : void { //Dem thoi gian cho moi thao tac
			if (finishTimer.currentCount == finishTimer.repeatCount) {
				finishTimer.stop() ;
				var mc : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("IconFinished")) ;
				while (mc != null) {
					Cocaro.mainStage_mc.removeChild(mc) ;
					mc = MovieClip(Cocaro.mainStage_mc.getChildByName("IconFinished")) ;
				}
				Xacnhan() ;
			}
		}
		
		public static function CheckPlay(hor : int, ver : int, oldHor : int, oldVer : int, sym : String, stt : int, csOld : int) : Boolean {
			//Cocaro.tf.appendText("=>CheckPlay") ;
			if (ArrOldPlay == null) return true ;
			//Cocaro.tf.appendText("=>Length:" + ArrOldPlay.length) ;
			if (ArrOldPlay.length < 14) return true ;
			//Cocaro.tf.appendText("==>Check:" + ArrOldPlay.length) ;
			var colorReturn : int = ArrCheck[hor][ver] ;
			ArrCheck[hor][ver] = MyColor ;
			ArrCheck[oldHor][oldVer] = 0 ;
			currTable.ArrPlayers[currPosition].ArrChess[csOld].Hor = hor ;
			currTable.ArrPlayers[currPosition].ArrChess[csOld].Ver = ver ;
			var count : int = 0 ;
			var countChieu : int =  0 ;
			if (Tg_Controller.CheckChieuTuong()) {
				//Cocaro.tf.appendText("=>Chieutuong") ;
				for (var i:int = ArrOldPlay.length - 1; i > ArrOldPlay.length - 14; i--) 
				{
					//Cocaro.tf.appendText("=>S:" + ArrOldPlay[i].S + "-" + sym + "*" + ArrOldPlay[i].H + "-" + hor + "*" + ArrOldPlay[i].V + "-" + ver) ;
					if ((ArrOldPlay[i].Name == Cocaro.currPlayer.PlayerName) && (ArrOldPlay[i].S == sym) && (ArrOldPlay[i].Stt == stt)
						&& (ArrOldPlay[i].H == hor) && (ArrOldPlay[i].V == ver)) {
							count = count + 1 ;
							if (ArrOldPlay[i].Chieu) countChieu = countChieu + 1 ;
						}
				}
			}			
			ArrCheck[hor][ver] = colorReturn ;
			ArrCheck[oldHor][oldVer] = MyColor ;
			currTable.ArrPlayers[currPosition].ArrChess[csOld].Hor = oldHor ;
			currTable.ArrPlayers[currPosition].ArrChess[csOld].Ver = oldVer ;
			//Cocaro.tf.appendText("==>CountChieu:" + countChieu + "-" + count) ;
			if (countChieu > 2) return false ;
			return true ;
		}
		
		public static function ForgedChess_Play(hor : int, ver : int, oldHor : int, oldVer : int, csOld : int) : Boolean {
			var isPlay : Boolean = true ;
			var colorReturn : int = ArrCheck[hor][ver] ;
			ArrCheck[hor][ver] = MyColor ;
			ArrCheck[oldHor][oldVer] = 0 ;
			currTable.ArrPlayers[currPosition].ArrChess[csOld].Hor = hor ;
			currTable.ArrPlayers[currPosition].ArrChess[csOld].Ver = ver ;
			//Cocaro.tf.appendText("=>Tuong:" + Tg_Controller.Check_LomatTuong()) ;
			if (Tg_Controller.Check_LomatTuong() != 0) {
				ArrCheck[hor][ver] = colorReturn ;
				ArrCheck[oldHor][oldVer] = MyColor ;
				currTable.ArrPlayers[currPosition].ArrChess[csOld].Hor = oldHor ;
				currTable.ArrPlayers[currPosition].ArrChess[csOld].Ver = oldVer ;
				Cocaro.mainStage_mc.Chat_mc.InTable_txt.htmlText += "<font color=\"#FF0000\">Mất tướng </font>\n" ;
				Cocaro.mainStage_mc.Chat_mc.All_txt.htmlText += "<font color=\"#FF0000\">Mất tướng </font>\n" ;
				//BasicFunction.ShowAlert("Nước chết!") ;				
				return false ;
			}
			ArrCheck[hor][ver] = colorReturn ;
			ArrCheck[oldHor][oldVer] = MyColor ;
			currTable.ArrPlayers[currPosition].ArrChess[csOld].Hor = oldHor ;
			currTable.ArrPlayers[currPosition].ArrChess[csOld].Ver = oldVer ;
			return true ;
		}
		
		public static function ForgedChess(hor : int, ver : int, oldHor : int, oldVer : int, csOld : int) : Boolean {
			var isFinish : Boolean = true ;
			var csReturn : int = -1 ;
			if (ArrCheck[hor][ver] != OtherColor) {
				if (ArrCheck[hor][ver] == MyColor) {
					for (var j:int = 0; j < currTable.ArrPlayers[currPosition].ArrChess.length; j++) 
					{
						if ((currTable.ArrPlayers[currPosition].ArrChess[j].Hor == hor) && (currTable.ArrPlayers[currPosition].ArrChess[j].Ver == ver)) {
							currTable.ArrPlayers[currPosition].ArrChess[j].Status == 1 ;
							csReturn = j ;	
						}
					}
				}
				ArrCheck[hor][ver] = OtherColor ;
				ArrCheck[oldHor][oldVer] = 0 ;
				currTable.ArrPlayers[csOther].ArrChess[csOld].Hor = hor ;
				currTable.ArrPlayers[csOther].ArrChess[csOld].Ver = ver ;
				if (!Tg_Controller.CheckChieuTuong()) { //Con nuoc di					
					isFinish = false ;
				}
				//Tra lai nuoc di				
				currTable.ArrPlayers[csOther].ArrChess[csOld].Hor = oldHor ;
				currTable.ArrPlayers[csOther].ArrChess[csOld].Ver = oldVer ;
				ArrCheck[hor][ver] = 0 ;
				ArrCheck[oldHor][oldVer] = OtherColor ;
				if (csReturn != -1) {
					ArrCheck[hor][ver] = MyColor ;
					currTable.ArrPlayers[currPosition].ArrChess[csReturn].Status == 0 ;
				}
			}
			return isFinish ;
		}
		
		public static function CheckFinishGame() : Boolean {
			var isFinish : Boolean = true ;
			//Cocaro.tf.appendText("=>StartCheck:") ;
			for (var i:int = 0; i < currTable.ArrPlayers[csOther].ArrChess.length; i++) 
			{
				var hor : int = currTable.ArrPlayers[csOther].ArrChess[i].Hor ;
				var ver : int = currTable.ArrPlayers[csOther].ArrChess[i].Ver ;
				//Cocaro.tf.appendText("=>Value:" + currTable.ArrPlayers[csOther].ArrChess.length + "-" + i + "-" + hor + "-" + ver + "-" + currTable.ArrPlayers[csOther].ArrChess[i].Symbol) ;
				if (currTable.ArrPlayers[csOther].ArrChess[i].Symbol == "Tg") {
					//{Nuoc di cua tuong					
					if (ver > 3) 
						isFinish = ForgedChess(hor, ver - 1, hor, ver, i) ;
					if (!isFinish) {
						//Cocaro.tf.appendText("=>Tuong trai") ;	
						return false ;
					}
					if (ver < 5) 
						isFinish = ForgedChess(hor, ver + 1, hor, ver, i) ;
					if (!isFinish) {
						//Cocaro.tf.appendText("=>Tuong phai") ;
						return false ;
					}
					if (hor > 0) 
						isFinish = ForgedChess(hor - 1, ver, hor, ver, i) ;
					if (!isFinish) {
						//Cocaro.tf.appendText("=>Tuong tren") ;
						return false ;
					}
					if (hor < 2) 
						isFinish = ForgedChess(hor + 1, ver, hor, ver, i) ;					
					if (!isFinish) {
						//Cocaro.tf.appendText("=>Tuong duoi") ;
						return false ;
					}
					//}
				} else if (currTable.ArrPlayers[csOther].ArrChess[i].Symbol == "S") {
					//{Nuoc di cua Sy
					if ((hor == 1) && (ver == 4)) {
						isFinish = ForgedChess(hor - 1, ver - 1, hor, ver, i) ;
						if (!isFinish) {
							//Cocaro.tf.appendText("=>Sy trai tren") ;
							return false ;
						}
						isFinish = ForgedChess(hor - 1, ver + 1, hor, ver, i) ;
						if (!isFinish) {
							//Cocaro.tf.appendText("=>Sy phai tren") ;
							return false ;
						}
						isFinish = ForgedChess(hor + 1, ver - 1, hor, ver, i) ;
						if (!isFinish) {
							//Cocaro.tf.appendText("=>Sy trai duoi") ;
							return false ;
						}
						isFinish = ForgedChess(hor + 1, ver + 1, hor, ver, i) ;
						if (!isFinish) {
							//Cocaro.tf.appendText("=>Sy phai duoi") ;
							return false ;
						}
					} else if (hor == 0) {
						if (ver == 3) {
							isFinish = ForgedChess(hor + 1, ver + 1, hor, ver, i) ;
							if (!isFinish) {
								//Cocaro.tf.appendText("=>Sy phai duoi") ;
								return false ;
							}
						}						
						if (ver == 5) {
							isFinish = ForgedChess(hor + 1, ver - 1, hor, ver, i) ;
							if (!isFinish) {
								//Cocaro.tf.appendText("=>Sy trai duoi") ;
								return false ;
							}
						}						
					} else if (hor == 2) {
						if (ver == 3) {
							isFinish = ForgedChess(hor - 1, ver + 1, hor, ver, i) ;
							if (!isFinish) {
								//Cocaro.tf.appendText("=>Sy phai tren") ;	
								return false ;
							}
						}						
						if (ver == 5) {
							isFinish = ForgedChess(hor - 1, ver - 1, hor, ver, i) ;
							if (!isFinish) {
								//Cocaro.tf.appendText("=>Sy trai tren") ;
								return false ;
							}
						}						
					} 
					//}
				} else if (currTable.ArrPlayers[csOther].ArrChess[i].Symbol == "T") {
					//{Nuoc di cua Tinh
					if ((hor >= 2) && (ver >= 2)) {
						isFinish = ForgedChess(hor - 2, ver - 2, hor, ver, i) ;
						if (!isFinish) {
							//Cocaro.tf.appendText("=>Tinh trai tren") ;
							return false ;
						}
					}
					if ((hor >= 2) && (ver < 8)) {
						isFinish = ForgedChess(hor - 2, ver + 2, hor, ver, i) ;
						if (!isFinish) {
							//Cocaro.tf.appendText("=>Tinh phai tren") ;
							return false ;
						}
					}
					if ((hor < 2) && (ver < 8)) {
						isFinish = ForgedChess(hor + 2, ver + 2, hor, ver, i) ;
						if (!isFinish) {
							//Cocaro.tf.appendText("=>Tinh phai duoi") ;
							return false ;
						}
					}
					if ((hor < 2) && (ver >= 2)) {
						isFinish = ForgedChess(hor + 2, ver - 2, hor, ver, i) ;
						if (!isFinish) {
							//Cocaro.tf.appendText("=>Tinh trai duoi") ;
							return false ;
						}
					}					
					//}
				} else if (currTable.ArrPlayers[csOther].ArrChess[i].Symbol == "M") {
					//{Nuoc di cua Ma
					//Tren Phai
					if ((hor > 1) && (ver < 8)) {
						if (ArrCheck[hor - 1][ver] == 0) {
							isFinish = ForgedChess(hor - 2, ver + 1, hor, ver, i) ;
							if (!isFinish) {
								//Cocaro.tf.appendText("=>Ma tren phai") ;							
								return false ;
							}
						}
					}
					//Tren Trai
					if ((hor > 1) && (ver > 0)) {
						if (ArrCheck[hor - 1][ver] == 0) {
							isFinish = ForgedChess(hor - 2, ver - 1, hor, ver, i) ;
							if (!isFinish) {
								//Cocaro.tf.appendText("=>Ma tren trai") ;
								return false ;
							}
						}
					}
					//Trai Tren
					if ((hor > 0) && (ver > 1)) {
						if (ArrCheck[hor][ver - 1] == 0) {
							isFinish = ForgedChess(hor - 1, ver - 2, hor, ver, i) ;
							if (!isFinish) {
								//Cocaro.tf.appendText("=>Ma trai tren") ;
								return false ;
							}
						}
					}
					//Trai duoi
					if ((hor < 9) && (ver > 1)) {
						if (ArrCheck[hor][ver - 1] == 0) {
							isFinish = ForgedChess(hor + 1, ver - 2, hor, ver, i) ;
							if (!isFinish) {
								//Cocaro.tf.appendText("=>Ma trai duoi") ;							
								return false ;
							}
						}
					}
					//Duoi Trai
					if ((hor < 8) && (ver > 0)) {
						if (ArrCheck[hor + 1][ver] == 0) {
							isFinish = ForgedChess(hor + 2, ver - 1, hor, ver, i) ;
							if (!isFinish) {
								//Cocaro.tf.appendText("=>Ma duoi trai") ;							
								return false ;
							}
						}
					}
					//Duoi Phai
					if ((hor < 8) && (ver < 8)) {
						if (ArrCheck[hor + 1][ver] == 0) {
							isFinish = ForgedChess(hor + 2, ver + 1, hor, ver, i) ;
							if (!isFinish) {
								//Cocaro.tf.appendText("=>Ma duoi phai") ;							
								return false ;
							}
						}
					}
					//Phai Duoi
					if ((hor < 9) && (ver < 7)) {
						if (ArrCheck[hor][ver + 1] == 0) {
							isFinish = ForgedChess(hor + 1, ver + 2, hor, ver, i) ;
							if (!isFinish) {
								//Cocaro.tf.appendText("=>Ma phai duoi") ;							
								return false ;
							}
						}
					}
					//Phai Tren
					if ((hor > 0) && (ver < 7)) {
						if (ArrCheck[hor][ver + 1] == 0) {
							isFinish = ForgedChess(hor - 1, ver + 2, hor, ver, i) ;
							if (!isFinish) {
								//Cocaro.tf.appendText("=>Ma phai tren") ;
								return false ;
							}
						}
					}					
					//}
				} else if (currTable.ArrPlayers[csOther].ArrChess[i].Symbol == "X") {
					//{Nuoc di cua Xe
					var horChay : int = hor ;
					var verChay : int = ver ;
					while (horChay > 0) {
						horChay = horChay - 1 ;
						if ((ArrCheck[horChay][verChay] == 0) || (ArrCheck[horChay][verChay] == MyColor)) {
							isFinish = ForgedChess(horChay, verChay, hor, ver, i) ;
							if (!isFinish) {
								//Cocaro.tf.appendText("=>Xe len tren") ;
								return false ;
							}
						} else break ;						
					}
					horChay = hor ;
					verChay = ver ;
					while (horChay < 9) {
						horChay = horChay + 1 ;
						if ((ArrCheck[horChay][verChay] == 0) || (ArrCheck[horChay][verChay] == MyColor)) {
							isFinish = ForgedChess(horChay, verChay, hor, ver, i) ;
							if (!isFinish) {
								//Cocaro.tf.appendText("=>Xe xuong duoi") ;
								return false ;
							}
						} else break ;
					}
					horChay = hor ;
					verChay = ver ;
					while (verChay < 8) {
						verChay = verChay + 1 ;
						if ((ArrCheck[horChay][verChay] == 0) || (ArrCheck[horChay][verChay] == MyColor)) {
							isFinish = ForgedChess(horChay, verChay, hor, ver, i) ;
							if (!isFinish) {
								//Cocaro.tf.appendText("=>Xe Sang trai") ;
								return false ;
							}
						} else break ;
					}
					horChay = hor ;
					verChay = ver ;
					while (verChay > 0) {
						verChay = verChay - 1 ;
						if ((ArrCheck[horChay][verChay] == 0) || (ArrCheck[horChay][verChay] == MyColor)) {
							isFinish = ForgedChess(horChay, verChay, hor, ver, i) ;
							if (!isFinish) {
								//Cocaro.tf.appendText("=>Xe Sang Phai") ;
								return false ;
							}
						} else break ;
					}
					//}
				} else if (currTable.ArrPlayers[csOther].ArrChess[i].Symbol == "P") {
					//{Nuoc di cua Phao
					//Cocaro.tf.appendText("=>Phao") ;
					var horChay : int = hor ;
					var verChay : int = ver ;
					while (horChay > 0) {
						horChay = horChay - 1 ;
						if (ArrCheck[horChay][verChay] == 0) {
							isFinish = ForgedChess(horChay, verChay, hor, ver, i) ;
							if (!isFinish) {
								Cocaro.tf.appendText("=>Phao len tren") ;
								return false ;
							}
						} else {
							while (horChay > 0) {
								horChay = horChay - 1 ;
								if (ArrCheck[horChay][verChay] == MyColor)	{
									isFinish = ForgedChess(horChay, verChay, hor, ver, i) ;
									if (!isFinish) {
										//Cocaro.tf.appendText("=>Phao an len tren") ;									
										return false ;
									}
								} else if (ArrCheck[horChay][verChay] != 0) break ;
							}
							break ;
						}
					}
					horChay = hor ;
					verChay = ver ;
					while (horChay < 9) {
						horChay = horChay + 1 ;
						if (ArrCheck[horChay][verChay] == 0) {
							isFinish = ForgedChess(horChay, verChay, hor, ver, i) ;
							if (!isFinish) {
								//Cocaro.tf.appendText("=>Phao xuong duoi") ;
								return false ;
							}
						} else {
							while (horChay < 9) {
								horChay = horChay + 1 ;
								if (ArrCheck[horChay][verChay] == MyColor) {
									isFinish = ForgedChess(horChay, verChay, hor, ver, i) ;
									if (!isFinish) {
										//Cocaro.tf.appendText("=>Phao an xuong duoi") ;
										return false ;
									}
								} else if (ArrCheck[horChay][verChay] != 0) break ;								
							}
							break ;
						}
					}
					horChay = hor ;
					verChay = ver ;
					while (verChay < 8) {
						verChay = verChay + 1 ;
						if (ArrCheck[horChay][verChay] == 0) {
							isFinish = ForgedChess(horChay, verChay, hor, ver, i) ;
							if (!isFinish) {
								//Cocaro.tf.appendText("=>Phao sang trai") ;
								return false ;
							}
						} else {
							while (verChay < 8) {
								verChay = verChay + 1 ;
								if (ArrCheck[horChay][verChay] == MyColor) {
									isFinish = ForgedChess(horChay, verChay, hor, ver, i) ;
									if (!isFinish) {
										//Cocaro.tf.appendText("=>Phao an sang trai") ;										
										return false ;
									}
								} else if (ArrCheck[horChay][verChay] != 0) break ;							
							}
							break ;
						}
					}
					horChay = hor ;
					verChay = ver ;
					while (verChay > 0) {
						verChay = verChay - 1 ;
						if (ArrCheck[horChay][verChay] == 0) {
							isFinish = ForgedChess(horChay, verChay, hor, ver, i) ;
							if (!isFinish) {
								//Cocaro.tf.appendText("=>Phao sang phai") ;
								return false ;
							}
						} else {
							while (verChay > 0) {
								verChay = verChay - 1 ;
								if (ArrCheck[horChay][verChay] == MyColor) {
									isFinish = ForgedChess(horChay, verChay, hor, ver, i) ;
									if (!isFinish) {
										//Cocaro.tf.appendText("=>Phao an sang phai") ;											
										return false ;
									}
								} else if (ArrCheck[horChay][verChay] != 0) break ;								
							}
							break ;
						}
					}
					//}
				} else if (currTable.ArrPlayers[csOther].ArrChess[i].Symbol == "B") {
					//{Nuoc di cua tot
					if (hor > 4) {
						if (ver < 8)
							isFinish = ForgedChess(hor, ver + 1, hor, ver, i) ;
						if (ver > 0)
							isFinish = ForgedChess(hor, ver - 1, hor, ver, i) ;
						if (hor < 8)
							isFinish = ForgedChess(hor + 1, ver, hor, ver, i) ;
						if (!isFinish) {
							//Cocaro.tf.appendText("==>Tot Sang song") ;
							return false ;
						}
					} else {
						isFinish = ForgedChess(hor + 1, ver, hor, ver, i) ;
						if (!isFinish) {
							//Cocaro.tf.appendText("==>Tot chua Sang song") ;
							return false ;
						}
					}
					//}
				}
				//Cocaro.tf.appendText("==>i:" + i + "-" + isFinish) ;
				//if (!isFinish) break ;
			}
			//Cocaro.tf.appendText("==>Finish:" + isFinish) ;
			return true ;
		}
		
		public static function FinishedGame(str : String) {	
			if (mainTimer.running)
				mainTimer.stop() ;
			//tf.appendText("FG:" + str) ;
			var object : Array = (JSON.decode(str) as Array) ;
			for (var t:int = 0; t < currTable.ArrPlayers.length; t++) 
				currTable.ArrPlayers[t].Ready = false ;				
			currTable.Status = 2 ;
			var mcFinished : MovieClip ;			
			//tf.appendText("Length:" + object.length) ;
			for (var i:int = 0; i < object.length; i++) 
			{
				var item:Object = object[i];
				//tf.appendText("==>Name:" + item.N) ;
				var mc : MovieClip = MovieClip(new Mark_Discount()) ;
				mc.name = "IconFinished" ;
				if (item.N == Cocaro.currPlayer.PlayerName) {
					currTable.ArrPlayers[currPosition].Mark = item.AG ; //Diem moi
					Cocaro.mainStage_mc.Player1_mc.Mark_txt.text = BasicFunction.numberFormat(currTable.ArrPlayers[currPosition].Mark, 0, false, true);
					mc.x = 660 ;
					mc.y = 370 ;
					if (item.M > 0) {
						WinSound() ;
						mcFinished = MovieClip(new Thang()) ;
						currTable.ArrPlayers[currPosition].WinGames ++ ;
						currTable.ArrPlayers[currPosition].TotalGames ++ ;
						if (!Cocaro.currPlayer.IsCreateTable) {
							Cocaro.currPlayer.WinGames++ ;
							Cocaro.currPlayer.TotalGames++ ;						
						}
						MyWin++ ;
					} else if (item.M < 0) {
						LoseSound() ;
						currTable.ArrPlayers[currPosition].TotalGames ++ ;
						if (!Cocaro.currPlayer.IsCreateTable) 
							Cocaro.currPlayer.TotalGames++ ;
						mcFinished = MovieClip(new Thua()) ;
					} else {
						mcFinished = MovieClip(new Hoa()) ;
					}
					mcFinished.name = "IconFinished" ;
					mcFinished.x = 200 ;
					mcFinished.y = 260 ;
					//tf.appendText("==>FG:" + currTable.ArrPlayers[currPosition].TotalGames + "-" + currTable.ArrPlayers[currPosition].WinGames) ;
					if (currTable.ArrPlayers[currPosition].TotalGames != 0)
						Cocaro.mainStage_mc["Player1_mc"].Win_txt.text = (int)(currTable.ArrPlayers[currPosition].WinGames * 100 / currTable.ArrPlayers[currPosition].TotalGames) + "%" ;
					else 
						Cocaro.mainStage_mc["Player1_mc"].Win_txt.text = "0%" ;
					Cocaro.mainStage_mc.addChild(mcFinished) ;
				} else {
					currTable.ArrPlayers[csOther].Mark = item.AG ; //Diem moi
					Cocaro.mainStage_mc.Player2_mc.Mark_txt.text = BasicFunction.numberFormat(currTable.ArrPlayers[csOther].Mark, 0, false, true);
					mc.x = 660 ;
					mc.y = 110 ;
					if (item.M > 0) {
						currTable.ArrPlayers[csOther].WinGames ++ ;
						currTable.ArrPlayers[csOther].TotalGames ++ ;
						OtherWin++ ;
					} else if (item.M < 0) {
						currTable.ArrPlayers[csOther].TotalGames ++ ;						
					}
					if (currTable.ArrPlayers[csOther].TotalGames != 0)
						Cocaro.mainStage_mc["Player2_mc"].Win_txt.text = (int)(currTable.ArrPlayers[csOther].WinGames * 100 / currTable.ArrPlayers[csOther].TotalGames) + "%" ;
					else 
						Cocaro.mainStage_mc["Player2_mc"].Win_txt.text = "0%" ;
				}
				//tf.appendText("==>1") ;
				//if (item.M > 0) mc.Mark_txt.text = "+ " + item.M.toString() ;
				//else mc.Mark_txt.text = item.M.toString() ;
				mc.Mark_txt.text = BasicFunction.numberFormat(item.M, 0, false, true) ;
				var formatText : TextFormat = new TextFormat() ;				
				formatText.font = "Tahoma" ;
				formatText.bold = true ;
				mc.Mark_txt.setTextFormat(formatText) ;	
				Cocaro.mainStage_mc.addChild(mc) ;
			}
			Cocaro.mainStage_mc.Player2_mc.txtTime_mc.text = "" ;
			Cocaro.mainStage_mc.Player1_mc.txtTime_mc.text = "" ;
			//Cocaro.mainStage_mc.btn_Xinthua.visible = false ;
			Cocaro.mainStage_mc.btn_Cauhoa.visible = false ;
			//Cocaro.mainStage_mc.btn_Xindilai.visible = false ;			
			if (currPosition == 0) {
				Cocaro.mainStage_mc.Bangtyso_mc.Player1_txt.text = MyWin.toString() ;
				Cocaro.mainStage_mc.Bangtyso_mc.Player2_txt.text = OtherWin.toString() ;
			} else {
				Cocaro.mainStage_mc.Bangtyso_mc.Player2_txt.text = MyWin.toString() ;
				Cocaro.mainStage_mc.Bangtyso_mc.Player1_txt.text = OtherWin.toString() ;
			}			
			if (Cocaro.currPlayer.IsCreateTable) {
				Cocaro.mainStage_mc.Player2_mc.btnKick_mc.visible = true ;
				var obj:Object = new Object();
				obj.evt = "rtable";
				Cocaro.mainSocket.sendData(JSON.encode(obj));
			} 
			FinishedTimer() ;
		}
		
		/**********************************************Finish Game ***********************************************/
		public static function StartTimerAutoExit(evt:TimerEvent) : void { //Dem thoi gian an ==> Tu dong click player
			if ((Cocaro.mainStage_mc.currentFrame == 5) && (!Cocaro.currPlayer.IsCreateTable))
				Cocaro.mainStage_mc.txtTimeReady.text = 20 - TimerAutoExit.currentCount ;
			if ((TimerAutoExit.currentCount == TimerAutoExit.repeatCount) && (Cocaro.mainStage_mc.currentFrame == 5) && (!Cocaro.currPlayer.IsCreateTable)) {				
				if ((currTable.ArrPlayers[currPosition].Ready == false) && (!Cocaro.isView)) {
					var leaveRequestPacket:LeaveRequestPacket = new LeaveRequestPacket();
					leaveRequestPacket.tableid = Cocaro.tableID;
					Cocaro.mainSocket.send(leaveRequestPacket);;
				}
			}
			if (TimerAutoExit.currentCount == TimerAutoExit.repeatCount)
				TimerAutoExit.stop() ;
		}
		
		public static function Xacnhan() {
			Cocaro.tf.appendText("=>Finish") ;
			currTable.Status = 0 ; //Chuyen trang thai ban ve trang thai cho
			if (Cocaro.mainStage_mc.currentFrame != 5) return ;			
			AllowExit = true ;
			if (ArrExitTable != null) 
				if (ArrExitTable.length != 0) {
					for (var j:int = 0; j < ArrExitTable.length; j++) 
					{
						ExitTable(ArrExitTable[j]) ;					
					}
					ArrExitTable = new Array() ;
				}
			Cocaro.tf.appendText("=>1:" + ArrUpdateMoney.length) ;
			if (ArrUpdateMoney != null) {
				if (ArrUpdateMoney.length != 0) {
					for (var i:int = 0; i < ArrUpdateMoney.length; i++) 
					{
						UpdateMoney_InTable(ArrUpdateMoney[i]) ;					
					}
					ArrUpdateMoney = new Array() ;
				}
			}
			if ((!Cocaro.currPlayer.IsCreateTable) && (!Cocaro.isView)){
				if (!currTable.ArrPlayers[currPosition].Ready)
					AddBtnReady() ;
			} else {
				var obj:Object = new Object();
				obj.evt = "rtable";
				Cocaro.mainSocket.sendData(JSON.encode(obj));				
			}		
			Cocaro.mainStage_mc.btnBackTable.visible = true ;			
			AName = "";
			if (Cocaro.currPlayer.IsCreateTable) {					
				if (currTable.CurrentPlayer > 1)
					Cocaro.mainStage_mc.Player2_mc.btnKick_mc.visible = true ;				
				Cocaro.mainStage_mc.Setting_btn.visible = true ;
			}
			if (!Cocaro.isView && !Cocaro.currPlayer.IsCreateTable) {
				Cocaro.mainStage_mc.txtTimeReady.visible = true ;
				Cocaro.mainStage_mc.txtTimeReady.text = "20" ;
				//Khoi tao thoi gian dem de AutoClick
				if (TimerAutoExit.running) TimerAutoExit.stop() ;
				TimerAutoExit = new Timer(1000) ;
				TimerAutoExit.repeatCount = 20 ;
				TimerAutoExit.addEventListener(TimerEvent.TIMER, StartTimerAutoExit);				
				TimerAutoExit.start() ;
			}
		}
		/******************************************* Finish Game *********************************************/
		public static function clickExitTable(evt : MouseEvent):void {	//Kick nut Exit ban		
			if (currTable.TableID == 0) return ;
			Cocaro.isSend = true ;
			Cocaro.isView = false ;
			var leaveRequestPacket:LeaveRequestPacket = new LeaveRequestPacket();
			////Cocaro.tf.appendText("ID:" + currTable.TableID + "-" + Cocaro.tableID) ;
			leaveRequestPacket.tableid = Cocaro.tableID;
			Cocaro.mainSocket.send(leaveRequestPacket);;
		}
		
		public static function ExitTable(str : String) { //Xu ly khi Server tra ve thong tin co nguoi thoat khoi ban
			//Cocaro.tf.appendText("Exit Table:") ;
			if (!AllowExit) {
				ArrExitTable.push(str)
				return ;
			}				
			Cocaro.SoundKick() ;
			EmotionFunction.RemoveEmotion() ;
			try 
			{
				var cs : int = 0;
				var name : String = "" ;
				if (str == Cocaro.tableID.toString()) 
					name = Cocaro.currPlayer.PlayerName ;
				else
					name = str ;
				//Cocaro.tf.appendText("=>Exit Table:" + name + "-" + str) ;		
				if (name == Cocaro.currPlayer.PlayerName) { //Neu dung la user thoat
					//Cham dut het thoi gian
					if (mainTimer != null)
						if (mainTimer.running)
							mainTimer.stop() ;					
					RemoveBtnRead() ;
					RemoveBtnInitGame() ;
					for (var i:int = 0; i <currTable.ArrPlayers.length; i++) 
					{
						if (currTable.ArrPlayers[i].PlayerName == Cocaro.currPlayer.PlayerName) {
							Cocaro.currPlayer.Mark = currTable.ArrPlayers[i].Mark ;
							break ;
						}
					}
					//Remove giao dien
					var mcRemove : MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Chess")) ;
					while (mcRemove != null) {
						Cocaro.mainStage_mc.removeChild(mcRemove) ;
						mcRemove = MovieClip(Cocaro.mainStage_mc.getChildByName("Chess")) ;
					}
					//Remove cac popup
					mcRemove = MovieClip(Cocaro.mainStage_mc.getChildByName("Confirm")) ;
					while (mcRemove != null) {
						Cocaro.mainStage_mc.removeChild(mcRemove) ;
						mcRemove = MovieClip(Cocaro.mainStage_mc.getChildByName("Confirm")) ;
					}
					Cocaro.htmlPrivateChat = Cocaro.mainStage_mc.Chat_mc.Private_txt.htmlText ;
					Cocaro.htmlWorldChat = Cocaro.mainStage_mc.Chat_mc.World_txt.htmlText ;
					Cocaro.htmlAllChat = Cocaro.mainStage_mc.Chat_mc.All_txt.htmlText ;
					//Khoi tao lai toan bo			
					currTable = new ObjectTable() ;
					//{Tro ve khung tao ban
					//Cocaro.tf.appendText("=>1") ;
					Cocaro.mainStage_mc.gotoAndStop(4);
					Cocaro.isView = false ;
					//Cocaro.mainStage_mc.Box_btn.addEventListener(MouseEvent.CLICK, BoxFunction.Box_Click) ;
					Cocaro.mainStage_mc.Mail_btn.addEventListener(MouseEvent.CLICK, MailFunction.Mail_Click) ;
					//Cocaro.mainStage_mc.Group_btn.addEventListener(MouseEvent.CLICK, GroupFunction.Group_Click) ;
					Cocaro.mainStage_mc.Bank_btn.addEventListener(MouseEvent.CLICK, BankFunction.Bank_Click) ;
					//Cocaro.mainStage_mc.Shop_btn.addEventListener(MouseEvent.CLICK, ShopFunction.Shop_Click) ;
					//Cocaro.mainStage_mc.Football_btn.addEventListener(MouseEvent.CLICK, FootballFunction.Football_Click) ;
					//Cocaro.mainStage_mc.Help_btn.addEventListener(MouseEvent.CLICK, HelpFunction.Help_Click) ;
					//Cocaro.mainStage_mc.Quest_btn.addEventListener(MouseEvent.CLICK, QuestFunction.Quest_Click) ;
					//Cocaro.mainStage_mc.Lock_btn.addEventListener(MouseEvent.CLICK, LockFunction.Lock_Click) ;
					//Cocaro.mainStage_mc.Profile_btn.addEventListener(MouseEvent.CLICK, ProfileFunction.Profile_Click) ;
					if (Cocaro.currPlayer.CountMsg > 0) {
						Cocaro.mainStage_mc.Mail_btn.CountMsg_txt.text = Cocaro.currPlayer.CountMsg ;
						Cocaro.mainStage_mc.Mail_btn.CountMsg_txt.visible = true ;
						Cocaro.mainStage_mc.Mail_btn.gotoAndStop(2) ;
					} else {
						Cocaro.mainStage_mc.Mail_btn.CountMsg_txt.visible = false ;
						Cocaro.mainStage_mc.Mail_btn.gotoAndStop(1) ;
					}
					//Cocaro.tf.appendText("=>2") ;
					ChatFunction.InitChat_OutTable() ;
					Cocaro.mainStage_mc.btnCreateTable.addEventListener(MouseEvent.CLICK, ProcessOutTable_Frame2.clickCreateTable) ;			
					Cocaro.mainStage_mc.btnComeInTable.addEventListener(MouseEvent.CLICK, ProcessOutTable_Frame2.clickComeInTable) ;
					Cocaro.mainStage_mc.btnPlayGame.addEventListener(MouseEvent.CLICK, ProcessOutTable_Frame2.btnPlayGame) ;
					Cocaro.mainStage_mc.btnBack_mc.addEventListener(MouseEvent.CLICK, ProcessOutTable_Frame2.btnBack_mc_Click);
					if (Cocaro.isBegin) {
						Cocaro.mainStage_mc.btnCreateTable.visible = false ;
						Cocaro.mainStage_mc.btnComeInTable.visible = false ;				
						Cocaro.mainStage_mc.btnPlayGame.visible = false ;	
					}
					//Cocaro.tf.appendText("=>3") ;
					//{ Hien thi thong tin nguoi choi
					if ((Cocaro.currPlayer.TinyURL != "") && (Cocaro.currPlayer.TinyURL != null)) {
						var imageLoader:Loader = new Loader();
						var image:URLRequest = new URLRequest(Cocaro.currPlayer.TinyURL) ;
						imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaderBig);				
						imageLoader.name = "BigAW" ;
						imageLoader.load(image);
						function onLoaderBig(evt:Event)
						{
							imageLoader.width = 50 ;
							imageLoader.height = 50 ;
							if (Cocaro.mainStage_mc.Avatar_mc.numChildren > 1) {
								Cocaro.mainStage_mc.Avatar_mc.removeChildAt(1) ;
							}
							Cocaro.mainStage_mc.Avatar_mc.addChild(imageLoader);
						}				
					}
					Cocaro.mainStage_mc.Profile_mc.Name_txt.text = Cocaro.currPlayer.PlayerName ;
					Cocaro.mainStage_mc.Profile_mc.Mark_txt.text = BasicFunction.numberFormat(Cocaro.currPlayer.Mark, 0, false, true) + " AG" ;			
					Cocaro.mainStage_mc.Profile_mc.MarkLQ_txt.text = BasicFunction.numberFormat(Cocaro.currPlayer.MarkLQ, 0, false, true) + " LQ" ;			
					Cocaro.mainStage_mc.Profile_mc.Name_txt.setTextFormat(Cocaro.formatTextMain) ;
					Cocaro.mainStage_mc.Profile_mc.Mark_txt.setTextFormat(Cocaro.formatTextMain) ;			
					Cocaro.mainStage_mc.Profile_mc.MarkLQ_txt.setTextFormat(Cocaro.formatTextMain) ;
					Cocaro.mainStage_mc.Profile_mc.Vip_mc.visible = true ;
					Cocaro.mainStage_mc.Profile_mc.Vip_mc.gotoAndStop(Cocaro.currPlayer.VIP + 1) ;
					//Cocaro.tf.appendText("=>4") ;
					var positionKhung : int = 1 ;
					if (Cocaro.currPlayer.Mark > Cocaro.vip) {
						positionKhung = 3 ;
						Cocaro.formatAvatar_Name.color = 0xFFFFFF ;
					} else {
						if (Cocaro.currPlayer.Mark > Cocaro.vipSilver)
							positionKhung = 2 ;
						Cocaro.formatAvatar_Name.color = 0xE5DFE9 ;
					}
					Cocaro.mainStage_mc.Profile_mc.Name_txt.setTextFormat(Cocaro.formatAvatar_Name) ;
					Cocaro.mainStage_mc.Profile_mc.Mark_txt.setTextFormat(Cocaro.formatAvatar_Mark) ;	
					Cocaro.mainStage_mc.Profile_mc.MarkLQ_txt.setTextFormat(Cocaro.formatAvatar_MarkLQ) ;
					Cocaro.mainStage_mc.Profile_mc.BG_Avatar.gotoAndStop(positionKhung) ;
					//{ Hien thi danh sach phong choi
					for (var o:int = 0; o < ProcessOutTable_Frame2.movieRooms.length; o++) 
					{
						Cocaro.mainStage_mc.ListRoom_mc.addChild(ProcessOutTable_Frame2.movieRooms[o]) ;
					}
					//}
					//Cocaro.tf.appendText("=>EX:" + Cocaro.currPlayer.Mark + "-" + Cocaro.LastMarkTable) ;
					//{ Check Tien hien Popup
					if (Cocaro.currPlayer.Mark < Cocaro.LastMarkTable) {
						//Cocaro.tf.appendText("=>X:" + Cocaro.currPlayer.X) ;
						var tmpTB:MovieClip = MovieClip(new Popup_TangAG()) ;
						if (Cocaro.currPlayer.X == 1) {							
							tmpTB.txtThongbao.text = "Bạn không đủ AG tham gia bàn chơi. Bạn được tặng 1 thẻ X2 (nhân đôi giá trị thẻ nạp đầu tiên trong ngày hôm nay). Bạn muốn chơi tiếp hay nạp thêm AG?" ;
						} else {
							tmpTB.txtThongbao.text = "Bạn không đủ AG tham gia bàn chơi. Bạn muốn chơi tiếp hay nạp thêm AG?" ;							
						}
						tmpTB.NapAG_btn.addEventListener(MouseEvent.CLICK, Cocaro.NapAG_Popup_Click) ;
						tmpTB.Choitiep_btn.addEventListener(MouseEvent.CLICK, Cocaro.Choitiep_Popup_Click) ;
						tmpTB.name = "TangAG" ;
						tmpTB.x = 0;
						tmpTB.y = 0;
						Cocaro.mainStage_mc.addChild(tmpTB) ;
					}
					//}
					//Cocaro.tf.appendText("=>6") ;
					Cocaro.tableID = 0;
					Cocaro.PARAM_CREATE_TABLE = new Array();
					ProcessOutTable_Frame2.clickRoom(Cocaro.currRoom.RoomID) ;
					return ;
				}
				//View lai cac Player
				//tf.appendText("==>length:" + currTable.ArrPlayers.length) ;
				for (var n:int = 0; n < currTable.ArrPlayers.length; n++) 
				{
					if (currTable.ArrPlayers[n].PlayerName == name) {
						cs = n ;
						currTable.ArrPlayers.splice(n, 1) ;
						break ;
					}
				}
				currTable.CurrentPlayer -- ;
				if (cs<currPosition)
					currPosition-- ;
				//{Hien thi len giao dien Player
				Cocaro.mainStage_mc.MarkUnit_txt.text = BasicFunction.numberFormat(currTable.MarkUnit, 0, false, true) + " \n Bàn:" + currTable.TableID ;			
				Cocaro.mainStage_mc.Player2_mc.visible = false ;
				LoadData_Player(currTable.ArrPlayers[currPosition].PlayerName, currTable.ArrPlayers[currPosition].Mark, currTable.ArrPlayers[currPosition].TinyURL, currTable.ArrPlayers[currPosition].Ready, currTable.ArrPlayers[currPosition].TotalGames, currTable.ArrPlayers[currPosition].WinGames, 1, currTable.ArrPlayers[currPosition].VIP) ;
				if (Cocaro.currPlayer.IsCreateTable){
					var countReady : int = 0 ;
					for (var o1:int = 0; o1 < currTable.ArrPlayers.length; o1++) 
					{
						if (currTable.ArrPlayers[o1].Ready == true) countReady++ ;				
					}
					if ((countReady == currTable.ArrPlayers.length) && (countReady > 1)) 
						AddBtnInitGame() ;
					else 
						RemoveBtnInitGame() ;
				}				
			} catch (ex : Error) {				
				//tf.appendText("Error:" + ex.getStackTrace());
			}		
		}
		/*****************Tang AG*****************/
		public static function UpdateMoney(ag : int) : void { //Hien thi thong bao Tang tien
			//Cocaro.tf.appendText("=>Tang tien 1:" + ag);
			Cocaro.currPlayer.Mark = ag + Cocaro.currPlayer.Mark;
			Cocaro.mainStage_mc.Profile_mc.Mark_txt.text = Cocaro.currPlayer.Mark ;
			BasicFunction.ShowAlert("Bạn được tặng " + ag + " AG!") ;			
		}
		
		public static function UpdateMoney_InTable(obj : Object) : void { //Hien thi thong bao Tang tien
			Cocaro.tf.appendText("=>Tangtien:" + obj.N + "-" + obj.M + "-" + AllowExit) ;
			var mark : Number = 0 ;
			if (!AllowExit) {
				ArrUpdateMoney.push(obj) ;
				return ;
			}
			Cocaro.tf.appendText("=>TangtienStart") ;
			if(Cocaro.currPlayer.PlayerName == obj.N) {
				var tmpTB:MovieClip = MovieClip(new Popup_TangAG()) ;
				if (obj.T == 1) {
					tmpTB.txtThongbao.text  = "Bạn được vừa được tặng " + obj.M + " AG.Làng Quạt xin tặng bạn 1 thẻ x2 (nhân đôi giá trị 1 thẻ nạp trong hôm nay). Chúc bạn chơi game vui vẻ!" ;
				} else 
					tmpTB.txtThongbao.text = "Bạn vừa được tặng " + obj.M + " AG" ;
				tmpTB.NapAG_btn.addEventListener(MouseEvent.CLICK, Cocaro.NapAG_Popup_Click) ;
				tmpTB.Choitiep_btn.addEventListener(MouseEvent.CLICK, Cocaro.Choitiep_Popup_Click) ;
				tmpTB.name = "TangAG" ;
				tmpTB.x = 0;
				tmpTB.y = 0;
				Cocaro.mainStage_mc.addChild(tmpTB) ;
			}
			if ((Cocaro.mainStage_mc.currentFrame == 4) && (Cocaro.currPlayer.PlayerName == obj.N)) {				
				Cocaro.currPlayer.Mark = obj.M + Cocaro.currPlayer.Mark;
				//Cocaro.mainStage_mc.Buttom_mc.PlayerMark_txt.text = Cocaro.currPlayer.Mark ;				
			} else if (Cocaro.mainStage_mc.currentFrame == 5) {
				for (var i:int = 0; i < currTable.ArrPlayers.length; i++) 
				{					
					if (currTable.ArrPlayers[i].PlayerName == obj.N) {
						currTable.ArrPlayers[i].Mark = currTable.ArrPlayers[i].Mark + obj.M ;
						mark = currTable.ArrPlayers[i].Mark ;
						if (obj.N == Cocaro.currPlayer.PlayerName)
							Cocaro.mainStage_mc.Player1_mc.Mark_txt.text = BasicFunction.numberFormat(currTable.ArrPlayers[i].Mark, 0, false, true) ;
						else
							Cocaro.mainStage_mc.Player2_mc.Mark_txt.text = BasicFunction.numberFormat(currTable.ArrPlayers[i].Mark, 0, false, true) ;						
						break ;
					}				
				}
				DisplayColorAgain() ;
			}/* else {
				Cocaro.currPlayer.Mark = obj.M + Cocaro.currPlayer.Mark;
			}*/
		}
		
		//public static function showMSG(str : String) : void { //Hien thi cac Thong bao Server gui ve
			//if ((Cocaro.mainStage_mc.currentFrame == 5) && (!AllowExit)) return ;
			//BasicFunction.RemoveHideLoading() ;
			//var tmpHide : MovieClip = MovieClip(new Hide());
			//tmpHide.name = "Hide";
			//tmpHide.x = 0 ;
			//tmpHide.y = 0 ;
			//tmpHide.alpha = 0 ;
			//Cocaro.mainStage_mc.addChild(tmpHide) ;
			//BasicFunction.ShowAlert(str) ;
		//}		
		/**************************** Setting lai ban choi *********************************************/
		public static function Close_Setting_Click(myEvent:MouseEvent) { //Kick Tat va bat tieng
			Cocaro.SoundKick() ;
			var mc :MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Setting")) ;
			if (mc == null) return ;
			BasicFunction.RemoveHideLoading() ;
			Cocaro.mainStage_mc.removeChild(mc) ;
		}
		public static function Check_ChangeMarkUnit(mark:int) {
			for (var i:int = 0; i < currTable.ArrPlayers.length; i++) 
			{
				if (currTable.ArrPlayers[i].Mark < 5 * mark) return false ; 
			}
			return true ;
		}
		public static function Xacnhan_Setting_Click(myEvent:MouseEvent) { //Kick Tat va bat tieng
			Cocaro.SoundKick() ;
			var mc :MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Setting")) ;
			if (mc == null) return ;
			var rbg : Object = mc.rb1.group ;
			var rbgT : Object = mc.TimePer1.group ;
			var rbgTT : Object = mc.TotalTime1.group ;
			//Cocaro.tf.appendText("==>1") ;
			if (rbg.selection == null) return ;
			var objSend : Object = new Object() ;
			objSend.evt = "setting" ;
			objSend.M = rbg.selection.value ;
			objSend.T = rbgT.selection.value ;
			objSend.TT = rbgTT.selection.value ;
			//Cocaro.tf.appendText("==>3") ;
			if (!Check_ChangeMarkUnit(objSend.M)) {
				BasicFunction.ShowAlert("Người chơi trong bàn không có đủ AG!") ;
				return ;
			}
			Cocaro.mainSocket.sendData(JSON.encode(objSend)) ;
			//Cocaro.tf.appendText("==>SendSetting:" + JSON.encode(objSend)) ;
			Cocaro.mainStage_mc.removeChild(mc) ;
		}
		public static function btnRule_mc_click(evt : MouseEvent) {
			var tmp:MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Setting")) ;
			var rbRule : Object = tmp.Rule1.group ;
			if (rbRule.selection.value == 2) {
				tmp.chkDanhnuoc.visible = true ;
				tmp.chkDanhnuoc.selected = true ;
			} else {
				tmp.chkDanhnuoc.visible = false ;
			}
		}
		
		public static function Setting(obj : Object) : void {
			//Cocaro.tf.appendText("=>Setting:" + obj.M) ;
			currTable.MarkUnit = obj.M ;
			currTable.TimePer = obj.T ;
			currTable.TotalTime = obj.TT ;			
			Cocaro.mainStage_mc.MarkUnit_txt.text = "Bàn:" + currTable.TableID + " - " + BasicFunction.numberFormat(currTable.MarkUnit, 0, false, true) ;			
			Cocaro.mainStage_mc.Chat_mc.InTable_txt.htmlText += "<font color=\"#FF0000\"> Mức cược mới:" + BasicFunction.numberFormat(currTable.MarkUnit, 0, false, true) + "- Thời gian:" + currTable.TimePer + "(giây)-" + currTable.TotalTime + "(phút)</font>\n" ;
			Cocaro.mainStage_mc.Chat_mc.All_txt.htmlText += "<font color=\"#FF0000\"> Mức cược mới:" + BasicFunction.numberFormat(currTable.MarkUnit, 0, false, true) + "- Thời gian:" + currTable.TimePer + "(giây)-" + currTable.TotalTime + "(phút)</font>\n" ;
			Cocaro.mainStage_mc.Chat_mc.InTable_txt.verticalScrollPosition = Cocaro.mainStage_mc.Chat_mc.InTable_txt.maxVerticalScrollPosition ;
			Cocaro.mainStage_mc.Chat_mc.All_txt.verticalScrollPosition = Cocaro.mainStage_mc.Chat_mc.All_txt.maxVerticalScrollPosition ;
			Cocaro.mainStage_mc.MarkUnit_txt.setTextFormat(Cocaro.formatTextMain) ;
			Cocaro.LastMarkTable = currTable.MarkUnit ;
		}
		public static function Setting_Click(evt : MouseEvent) :void { 
			if (Cocaro.mainStage_mc.currentFrame != 5) return ;
			if (currTable.Status == 1) return ;
			var tmpTable : MovieClip = MovieClip(new Popup_Setting()) ;
			tmpTable.Close_btn.addEventListener(MouseEvent.CLICK, Close_Setting_Click) ;
			tmpTable.Xacnhan_btn.addEventListener(MouseEvent.CLICK, Xacnhan_Setting_Click) ;			
			var formatText : TextFormat = new TextFormat() ;				
			formatText.font = "Tahoma" ;
			formatText.bold = true ;
			formatText.color = 0xFFFF00 ;
			formatText.size = 12 ;
			tmpTable.rb1.setStyle("textFormat", formatText) ;
			tmpTable.rb2.setStyle("textFormat", formatText) ;
			tmpTable.rb3.setStyle("textFormat", formatText) ;
			tmpTable.rb4.setStyle("textFormat", formatText) ;
			tmpTable.rb5.setStyle("textFormat", formatText) ;
			tmpTable.rb6.setStyle("textFormat", formatText) ;
			tmpTable.TimePer1.setStyle("textFormat", formatText) ;
			tmpTable.TimePer2.setStyle("textFormat", formatText) ;
			tmpTable.TotalTime1.setStyle("textFormat", formatText) ;
			tmpTable.TotalTime2.setStyle("textFormat", formatText) ;
			tmpTable.TimePer1.enabled = true ;
			tmpTable.TimePer2.enabled = true ;
			tmpTable.TotalTime1.selected = true ;
			tmpTable.TotalTime2.selected = true ;
			//currTable.TimePer = object.TimePer ;
			//currTable.TotalTime = object.TotalTime ;
			if (currTable.TimePer == 120) 
				tmpTable.TimePer1.selected = true ;
			else 
				tmpTable.TimePer2.selected = true ;
			if (currTable.TotalTime == 15) 
				tmpTable.TotalTime1.selected = true ;
			else 
				tmpTable.TotalTime2.selected = true ;
			if (Cocaro.currLevel.MinMark == 0) {
				tmpTable.rb1.label = "1,000 AG" ;
				tmpTable.rb1.value = 1000 ;
				tmpTable.rb1.enabled = true ;
				tmpTable.rb2.label = "10,000 AG" ;
				tmpTable.rb2.value = 10000 ;
				tmpTable.rb2.enabled = true ;
				tmpTable.rb3.label = "100,000 AG" ;
				tmpTable.rb3.value = 100000 ;
				tmpTable.rb3.enabled = true ;
				tmpTable.rb4.label = "500,000 AG" ;
				tmpTable.rb4.value = 500000 ;
				tmpTable.rb4.enabled = true ;
				tmpTable.rb5.label = "1,000,000 AG" ;
				tmpTable.rb5.value = 1000000 ;
				tmpTable.rb5.enabled = true ;
				tmpTable.rb6.label = "5,000,000 AG" ;
				tmpTable.rb6.value = 5000000 ;
				tmpTable.rb6.enabled = true ;
				tmpTable.rb2.selected = true ;
				if (currTable.MarkUnit == 1000)
					tmpTable.rb1.selected = true ;
				else if (currTable.MarkUnit == 10000)
					tmpTable.rb2.selected = true ;
				else if (currTable.MarkUnit == 100000)
					tmpTable.rb3.selected = true ;
				else if (currTable.MarkUnit == 500000)
					tmpTable.rb4.selected = true ;
				else if (currTable.MarkUnit == 1000000)
					tmpTable.rb5.selected = true ;
				else if (currTable.MarkUnit == 5000000)
					tmpTable.rb6.selected = true ;
			}
			tmpTable.name = "Setting" ;
			tmpTable.x = 0 ;
			tmpTable.y = 0 ;
			Cocaro.mainStage_mc.addChild(tmpTable) ;
		}
	}
}